【问题标题】:NA values and extra rows when spreading repeated measures of multiple variables into wide format?将多个变量的重复测量扩展到宽格式时的NA值和额外行?
【发布时间】:2019-02-23 04:03:10
【问题描述】:

根据下面的数据(包含在dput 中),我为三个人提供了不同数量的重复经纬度位置,并希望使用dplyr 将它们传播为宽格式。

数据如下:

> head(Dat)
  IndIDII      IndYear  WintLat  WintLong
1 BHS_265 BHS_265-2015 47.61025 -112.7210
2 BHS_265 BHS_265-2016 47.59884 -112.7089
3 BHS_770 BHS_770-2016 42.97379 -109.0400
4 BHS_770 BHS_770-2017 42.97129 -109.0367
5 BHS_770 BHS_770-2018 42.97244 -109.0509
6 BHS_377 BHS_377-2015 43.34744 -109.4821

This post 提供了一个非常有帮助的巧妙解决方案。尽管如此,我无法获得我想要的结果。修改代码我有以下内容:

Dat %>%  
  group_by(IndIDII) %>%
  #Make YearNum (as intiger not calnader year) for each IndIDII
  mutate(YearNum = row_number()) %>% 
  gather(Group, LatLong, c(WintLat,  WintLong)) %>% 
  unite(GroupNew, YearNum, Group, sep = "-") %>% 
  spread(GroupNew, LatLong) %>% 
  as.data.frame()

这会产生几乎正确的结果,但每个 IndIDII 有多行,每行包含一年的纬度和经度。

  IndIDII      IndYear 1-WintLat 1-WintLong 2-WintLat 2-WintLong 3-WintLat 3-WintLong 4-WintLat 4-WintLong
1 BHS_265 BHS_265-2015  47.61025  -112.7210        NA         NA        NA         NA        NA         NA
2 BHS_265 BHS_265-2016        NA         NA  47.59884  -112.7089        NA         NA        NA         NA
3 BHS_377 BHS_377-2015  43.34744  -109.4821        NA         NA        NA         NA        NA         NA
4 BHS_377 BHS_377-2016        NA         NA  43.35559  -109.4445        NA         NA        NA         NA
5 BHS_377 BHS_377-2017        NA         NA        NA         NA  43.35195  -109.4566        NA         NA
6 BHS_377 BHS_377-2018        NA         NA        NA         NA        NA         NA  43.34765  -109.4892
7 BHS_770 BHS_770-2016  42.97379  -109.0400        NA         NA        NA         NA        NA         NA
8 BHS_770 BHS_770-2017        NA         NA  42.97129  -109.0367        NA         NA        NA         NA
9 BHS_770 BHS_770-2018        NA         NA        NA         NA  42.97244  -109.0509        NA         NA

我试图将IndIDII 的所有经纬度都放在一行(即宽格式)中,如下所示。 NA 值将在个人少于最大年数时出现。我怀疑问题出在GroupNew 变量上,并尝试了不同的选项,但无济于事......

Dat <- structure(list(IndIDII = c("BHS_265", "BHS_265", "BHS_770", "BHS_770", 
"BHS_770", "BHS_377", "BHS_377", "BHS_377", "BHS_377"), IndYear = c("BHS_265-2015", 
"BHS_265-2016", "BHS_770-2016", "BHS_770-2017", "BHS_770-2018", 
"BHS_377-2015", "BHS_377-2016", "BHS_377-2017", "BHS_377-2018"
), WintLat = c(47.6102519805014, 47.5988417247191, 42.9737859090909, 
42.9712914772727, 42.9724390816327, 43.3474354347826, 43.3555934579439, 
43.3519543396226, 43.3476466990291), WintLong = c(-112.720994832869, 
-112.708887595506, -109.039964727273, -109.036693522727, -109.050923061224, 
-109.482114456522, -109.444522149533, -109.45659254717, -109.489241553398
)), class = "data.frame", row.names = c(NA, -9L))

【问题讨论】:

    标签: r dplyr


    【解决方案1】:

    你快到了。 latlong 进入不同的行,因为它们的 IndYear 不同。由于您只在最终的data.frame 中为每个IndiDII 保留IndYear 的第一个值,因此添加IndYear = first(IndYear) 将为您提供所需的结果。

    Dat %>%  
        group_by(IndIDII) %>%
        mutate(YearNum = row_number(), IndYear = first(IndYear)) %>% 
        gather(Group, LatLong, c(WintLat,  WintLong)) %>% 
        unite(GroupNew, YearNum, Group, sep = "-") %>% 
        spread(GroupNew, LatLong) %>% 
        as.data.frame()
    
    #   IndIDII      IndYear 1-WintLat 1-WintLong 2-WintLat 2-WintLong 3-WintLat 3-WintLong 4-WintLat 4-WintLong
    # 1 BHS_265 BHS_265-2015  47.61025  -112.7210  47.59884  -112.7089        NA         NA        NA         NA
    # 2 BHS_377 BHS_377-2015  43.34744  -109.4821  43.35559  -109.4445  43.35195  -109.4566  43.34765  -109.4892
    # 3 BHS_770 BHS_770-2016  42.97379  -109.0400  42.97129  -109.0367  42.97244  -109.0509        NA         NA
    

    【讨论】:

      猜你喜欢
      • 2015-06-28
      • 2017-10-13
      • 1970-01-01
      • 1970-01-01
      • 2021-05-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多