1

假设我有一个包含空间列 (x,y)、时间列 (time) 和数据列 (value) 的数据框。我怎样才能正确地将其强制为 stars 对象使用st_as_stars

df <- expand_grid(time=1:10,
                  x=1:10,
                  y=1:10)
df$value <- 1:1000

这是数据框的原始顺序:

> head(df)
# A tibble: 6 x 4
   time     x     y value
  <int> <int> <int> <int>
1     1     1     1     1
2     1     1     2     2
3     1     1     3     3
4     1     1     4     4
5     1     1     5     5
6     1     1     6     6

当我尝试将数据框强制转换为stars对象时,此处错误地指定了某些内容:

s <- st_as_stars(df,
                 dimensions=st_dimensions(time=sort(unique(df$time)), 
                                          x=sort(unique(df$x)),
                                          y=sort(unique(df$y)), 
                                          point=TRUE),
                 dims=c('time','x','y'))

这个星星对象有问题。(1) 'y' 列被视为一个属性。

> s
stars object with 3 dimensions and 2 attributes
attribute(s):
       y            value       
 Min.   : 1.0   Min.   :   1.0  
 1st Qu.: 3.0   1st Qu.: 250.8  
 Median : 5.5   Median : 500.5  
 Mean   : 5.5   Mean   : 500.5  
 3rd Qu.: 8.0   3rd Qu.: 750.2  
 Max.   :10.0   Max.   :1000.0  
dimension(s):
     from to offset delta refsys point values x/y
time    1 10    0.5     1     NA    NA   NULL [x]
x       1 10   10.5    -1     NA    NA   NULL [y]
y       1 10    0.5     1     NA    NA   NULL    

并且 (2) 将星星对象转换回数据帧表明数据“值”不像原始数据帧中那样有序。

> head(as.data.frame(s))
  time  x   y y.1 value
1    1 10 0.5   1    91
2    2 10 0.5   1   191
3    3 10 0.5   1   291
4    4 10 0.5   1   391
5    5 10 0.5   1   491
6    6 10 0.5   1   591

如何正确地将数据框来回投射到stars对象?

stars对象和方法非常有用,但我很纳闷为什么我做不出这个看似简单的数据转换。任何帮助将不胜感激。

4

1 回答 1

0

你有没有尝试过

st_as_stars(df, dims = c("x", "y", "time"))

?

于 2021-05-27T06:04:46.287 回答