R语言 Package 数量:取消嵌套跟踪将生成类为NULL列表式对象

mtb9vblg  于 2022-12-06  发布在  其他
关注(0)|答案(2)|浏览(126)

我不确定这是amt还是tidyr问题,但是还没有amt标记,所以我们就这样做了。
处理嵌套轨迹时,任何计算(例如step_lengths)在嵌套的轨道中完成,在“data”列之外创建了一个新的“类似列表”的列,具有相同的行数。我不确定这是否是所需的结果,我宁愿将其添加到“data”列内的嵌套数据集中,但这很好。可以通过取消嵌套和重新嵌套来轻松解决然而,unnest会产生NULL类的类似列表的奇怪对象,尽管我已经设法找到了一种解决方法,将其重新转换为 Dataframe ,然后我需要再次使用make_tracks和nest返回到起点,这很烦人。

首先创建数据集

library(random)
test_df <- data.frame(id = rep(c("A", "B", "C"), each = 100), 
                  lon = runif(min = -9, max = -6, n = 300),
                  lat = runif(min = 51, max = 55, n = 300), 
                  covar1 = runif(min = 0, max = 100, n = 300), 
                  covar2 = c(randomStrings(n=300, len=3, unique = F, 
                                         upperalpha = T, loweralpha = F, 
                                         digits = F)))

现在让它成为嵌套轨道

test_tracks <- test_df %>% 
  make_track(lon, lat, 
             all_cols = T,
             crs = 4326, 
             check_duplicates = FALSE, 
             verbose = TRUE) %>% 
  nest(data = c(-id))

计算步长

test_tracks2 <- test_tracks %>% 
  mutate(sl = map(data, step_lengths))

这将生成一个嵌套的df,其中包含一个附加的“list”列sl

> str(test_tracks2, 2)
nested_track [3 × 3] (S3: nested_track/tbl_df/tbl/data.frame)
 $ id  : chr [1:3] "A" "B" "C"
 $ data:List of 3
 $ sl  :List of 3

取消嵌套

test_tracks3 <- test_tracks2 %>% 
  unnest(cols = c(data, sl))

这将生成一个列表,但其类为NULL

> class(test_tracks3)
[1] "NULL"
> str(test_tracks3)
List of 6
 $ id    : chr [1:300] "A" "A" "A" "A" ...
 $ x_    : num [1:300] -7.67 -7.96 -8.39 -7.26 -8.49 ...
 $ y_    : num [1:300] 54.3 53.6 54.8 51.9 52.2 ...
 $ covar1: num [1:300] 55.028 0.772 76.037 64.362 34.512 ...
 $ covar2: chr [1:300] "KRU" "RJL" "RVG" "BUW" ...
 $ sl    : num [1:300] 0.718 1.28 3.087 1.261 0.648 ...
 - attr(*, "class")= chr "NULL"
 - attr(*, "row.names")= int [1:300] 1 2 3 4 5 6 7 8 9 10 ...

我可以使用www.example.com将这个列表恢复为普通的dfdo.call,但是其他的dplyr或tibble方法将不起作用

x <- as.data.frame(do.call(cbind, test_tracks3))
> str(x)
'data.frame':   300 obs. of  6 variables:
 $ id    : chr  "A" "A" "A" "A" ...
 $ x_    : chr  "-7.67035690904595" "-7.96027435711585" "-8.38539077946916" "-7.25699410191737" ...
 $ y_    : chr  "54.2703927513212" "53.6132442755625" "54.8205336350948" "51.9466292150319" ...
 $ covar1: chr  "55.0277820788324" "0.772069441154599" "76.0366528760642" "64.3617564812303" ...
 $ covar2: chr  "KRU" "RJL" "RVG" "BUW" ...
 $ sl    : chr  "0.718259177377767" "1.27994983112401" "3.08749181012144" "1.26120128657495" ...

然而,它现在是一个普通的df,所有的变量都失去了格式(它们都是chr),这是不理想的。
知道为什么会发生这种情况以及如何解决它吗?

2hh7jdfx

2hh7jdfx1#

我不知道为什么会发生这种情况,但另一个解决办法是在制作轨道本身之前嵌套“id”。

test_df <- data.frame(id = rep(c("A", "B", "C"), each = 100), 
                      lon = runif(min = -9, max = -6, n = 300),
                      lat = runif(min = 51, max = 55, n = 300), 
                      covar1 = runif(min = 0, max = 100, n = 300), 
                      covar2 = c(randomStrings(n=300, len=3, unique = F, 
                                               upperalpha = T, loweralpha = F, 
                                               digits = F)))

test_tracks <- test_df %>% 
  nest(data = -id) %>%
  mutate(trk = map(data, function(d) {
    make_track(d, lon, lat, 
    all_cols = T,
    crs = 4326, 
    check_duplicates = FALSE, 
    verbose = TRUE)}))

test_tracks2 = test_tracks %>% 
  mutate(sl = map(trk, amt::step_lengths))

test_tracks3 <- test_tracks2 %>%
  select(-c(data)) %>%
  tidyr::unnest(cols=c(trk, sl))
cetgtptt

cetgtptt2#

我总是这样计算:

library(random)
library(amt)
test_df <- data.frame(id = rep(c("A", "B", "C"), each = 100), 
                      lon = runif(min = -9, max = -6, n = 300),
                      lat = runif(min = 51, max = 55, n = 300), 
                      covar1 = runif(min = 0, max = 100, n = 300), 
                      covar2 = c(randomStrings(n=300, len=3, unique = F, 
                                               upperalpha = T, loweralpha = F, 
                                               digits = F)))

trk <- test_df %>% 
  nest(data = -id) %>%
  mutate(data = map(data, ~ {
    make_track(
      .x, lon, lat, 
      all_cols = TRUE,
      crs = 4326, 
      check_duplicates = FALSE, 
      verbose = TRUE)}))

并对列表列的每个元素应用所需的函数。

trk1 <- trk %>% 
  mutate(data = map(data, ~ mutate(.x, sl = step_lengths(.x)))) %>% 
  unnest(cols = data)

不幸的是,trk1丢失了track_xy类。我为此提交了一个问题。

相关问题