我在R中创建了以下数据帧
df<-data.frame("ID"=c("A", "A", "A", "B", "B", "B"),"II"=c(NA, NA,
NA,1,2,3),"JJ"=c(1,2,3, NA, NA, NA), 'KK'=c(1,2,NA, NA, 5,6))
生成的数据帧如下
ID II JJ KK
A NA 1 1
A NA 2 2
A NA 3 NA
B 1 NA NA
B 2 NA 5
B 3 NA 6
我希望通过ID值对数据帧进行子集化,并将仅包含NA值的所有列替换为999.结果应如下所示
ID II JJ KK
A 999 1 1
A 999 2 2
A 999 3 NA
B 1 999 NA
B 2 999 5
B 3 999 6
我试过这段代码
library(dplyr)
df%>%
group_by(ID)%>%
select(II, JJ,KK)%>%
mutate_if(df[, colSums(is.na(df)) == nrow(df)]<999)
我收到以下错误
Adding missing grouping variables: `ID`
Error in tbl_if_vars(.tbl, .p, .env, ..., .include_group_vars =
.include_group_vars) :
length(.p) == length(tibble_vars) is not TRUE
解决办法:data.table解决方案:
library(data.table)
setDT(df)
df[, lapply(.SD,
function(col) if (all(is.na(col))) 999 else col),
by = ID]
# ID II JJ KK
# 1: A 999 1 1
# 2: A 999 2 2
# 3: A 999 3 NA
# 4: B 1 999 NA
# 5: B 2 999 5
# 6: B 3 999 6
我们遍历所有.SDcols列wioth lapply并替换它们,999如果所有的都是NA
暂无数据