当 R 中有多个值选项时,在每个 ID 中重复一个值

问题描述

我在 R 中有一个数据集,其中包含不同 ID 内的多个高度观测值。对于某些 ID,有几种不同的高度度量,而对于某些 ID,则只有一种。对于每个 ID 中的大多数观察值/行,缺少高度值(编码为 NA)。我想创建一个新变量,它采用每个 ID 可用的第一个高度度量,并为该 ID 的所有行/观察重复它(不同的 ID 具有不同的总行数)。我曾尝试使用填充、变异和命令,但我正在努力使其发挥作用。

目前我的数据是这样的:

data = data.frame(id = c(1,1,2,3,3),height = c(150,NA,148,152,151,NA))

# id height
# 1  1    150
# 2  1     NA
# 3  1     NA
# 4  2     NA
# 5  2    148
# 6  3     NA
# 7  3    152
# 8  3    151
# 9  3     NA

理想情况下,我希望能够添加一个变量 (height_filled),使其看起来像这样:

data = data.frame(id = c(1,NA),height_filled = c(150,150,152))

# id height height_filled
# 1  1    150           150
# 2  1     NA           150
# 3  1     NA           150
# 4  2     NA           148
# 5  2    148           148
# 6  3     NA           152
# 7  3    152           152
# 8  3    151           152
# 9  3     NA           152

非常感谢任何帮助!

解决方法

我会尝试以下方法。按id分组后,对na.omit使用height删除缺失值,使用first选择NA删除后第一个可用的高度。

library(dplyr)

data %>%
  group_by(id) %>%
  mutate(height_filled = first(na.omit(height)))

输出

     id height height_filled
  <dbl>  <dbl>         <dbl>
1     1    150           150
2     1     NA           150
3     1     NA           150
4     2     NA           148
5     2    148           148
6     3     NA           152
7     3    152           152
8     3    151           152
9     3     NA           152
,

我们可以按 'id'、arrange 'id' 和 'height' 中的 NA 分组并使用 cummax

library(dplyr)
data %>%
   group_by(id) %>%
   arrange(id,is.na(height)) %>% 
   mutate(height_filled = cummax(replace(height,is.na(height),0))) %>%
   ungroup

-输出

# A tibble: 9 x 3
#     id height height_filled
#  <dbl>  <dbl>         <dbl>
#1     1    150           150
#2     1     NA           150
#3     1     NA           150
#4     2    148           148
#5     2     NA           148
#6     3    152           152
#7     3    151           152
#8     3     NA           152
#9     3     NA           152

或者在按 'id' 分组的 'height' 上使用 max

data %>%
   group_by(id) %>%
   mutate(height_filled = max(height,na.rm = TRUE)) %>%
   ungroup

-输出

# A tibble: 9 x 3
#     id height height_filled
#  <dbl>  <dbl>         <dbl>
#1     1    150           150
#2     1     NA           150
#3     1     NA           150
#4     2     NA           148
#5     2    148           148
#6     3     NA           152
#7     3    152           152
#8     3    151           152
#9     3     NA           152
,

使用 data.table + firstna.omit 选项

setDT(data)[,height_filled := first(na.omit(height)),id]

给予

   id height height_filled
1:  1    150           150
2:  1     NA           150
3:  1     NA           150
4:  2     NA           148
5:  2    148           148
6:  3     NA           152
7:  3    152           152
8:  3    151           152
9:  3     NA           152

使用 ave 的基本 R 选项

transform(
  data,height_filled = ave(height,id,FUN = function(x) head(na.omit(x),1))
)

给予

  id height height_filled
1  1    150           150
2  1     NA           150
3  1     NA           150
4  2     NA           148
5  2    148           148
6  3     NA           152
7  3    152           152
8  3    151           152
9  3     NA           152

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...