如果基于相同前缀的其他列中是否存在值,如何将列中的NA替换为全0?例如,对于列A1,我只想将NA替换为0,其中列A2或A3是NONBLANK。我的真实数据有数百组列。
我的数据:
ID<-c(1,2,3,4,5,6,7,8)
A1<-c(1,NA,1,NA,1,1,1,NA)
A2<-c(1,NA,NA,1,NA,1,NA,NA)
A3<-c(1,NA,NA,NA,1,NA,NA,NA)
B1<-c(1,1,1,1,1,1,NA,1)
B2<-c(1,1,1,1,NA,1,NA,NA)
B3<-c(1,1,NA,NA,1,NA,NA,NA)
mydata<-cbind.data.frame(ID,A1,A2,A3,B1,B2,B3)
HAVE:
WANTED:
如果列A2或A3具有1,则0应该替换列A1中的NA。如果列A1或A3具有1,则A应该替换列A2中的NA,依此类推,如下所示:
答案 0 :(得分:4)
另一种方法是
mydata[, 2:4][is.na(mydata[, 2:4])] <- rep(NA^(rowSums(is.na(mydata[2:4])) == 3) - 1,
length(2:4))[is.na(mydata[, 2:4])]
mydata[, 5:7][is.na(mydata[, 5:7])] <- rep(NA^(rowSums(is.na(mydata[5:7])) == 3) - 1,
length(5:7))[is.na(mydata[, 5:7])]
mydata
ID A1 A2 A3 B1 B2 B3
1 1 1 1 1 1 1 1
2 2 NA NA NA 1 1 1
3 3 1 0 0 1 1 0
4 4 0 1 0 1 1 0
5 5 1 0 1 1 0 1
6 6 1 1 0 1 1 0
7 7 1 0 0 NA NA NA
8 8 NA NA NA 1 0 0
列值是硬编码的,对许多组没有帮助,所以遵循@ haboryme的技术,你可以做到
# group columns into list elements with lapply and grep
myCols <- lapply(c("A", "B"), function(i) grep(i, colnames(mydata)))
# loop through and make changes
for(i in myCols) {
mydata[, i][is.na(mydata[, i])] <- rep(NA^(rowSums(is.na(mydata[i])) == 3) - 1,
length(i))[is.na(mydata[, i])]
}
答案 1 :(得分:4)
使用lapply()
的自定义函数:如果提供任意数量的列,只要它们遵循这种具有单个字母的模式
func <- function(x){
df <- mydata[grepl(x, colnames(mydata))] # extract only the same letter columns
m <- !is.na(df) # create a logical matrix to know which all are NA's
i = which(rowSums(m)!=0) # if all had NA's then summ will be 0. so avoid that
df[i,][is.na(df[i,])] <- 0 # insert wherever NA's to be 0( but only in those rows decided above)
return(df)
}
data.frame(ID = mydata$ID,lapply(LETTERS[1:2], func))
# ID A1 A2 A3 B1 B2 B3
#1 1 1 1 1 1 1 1
#2 2 NA NA NA 1 1 1
#3 3 1 0 0 1 1 0
#4 4 0 1 0 1 1 0
#5 5 1 0 1 1 0 1
#6 6 1 1 0 1 1 0
#7 7 1 0 0 NA NA NA
#8 8 NA NA NA 1 0 0
答案 2 :(得分:3)
基础R
中的非精确答案,但似乎有效:
for(i in unique(gsub("\\d","",colnames(mydata)[-1]))){
mydata[apply(mydata[,grepl(i,colnames(mydata))],1,function(x) any(!is.na(x))),grepl(i,colnames(mydata))][is.na(mydata[apply(mydata[,grepl(i,colnames(mydata))],1,function(x) any(!is.na(x))),grepl(i,colnames(mydata))])]<-0
}
给出了:
ID A1 A2 A3 B1 B2 B3
1 1 1 1 1 1 1 1
2 2 NA NA NA 1 1 1
3 3 1 0 0 1 1 0
4 4 0 1 0 1 1 0
5 5 1 0 1 1 0 1
6 6 1 1 0 1 1 0
7 7 1 0 0 NA NA NA
8 8 NA NA NA 1 0 0
修改强>
我们的想法是从示例中的unique
,colnames(mydata)
和A
中提取B
个字母,方法是将数字(\\d
)替换为空白{""
1}}。
然后循环遍历这些字母以选择以它开头的列。这是grepl(i,colnames(mydata))
的作用
apply
用于获取至少(any()
)一个非NA值(!is.na()
)的行的向量:apply(mydata[,grepl(i,colnames(mydata))],1,function(x) any(!is.na(x)))
。
然后将所有内容组合在一起df[is.na(df))]<-0
,但df
对应于给定字母的列,以及应更换NA
的行。 />
df
将是:mydata[apply(mydata[,grepl(i,colnames(mydata))],1,function(x) any(!is.na(x))),grepl(i,colnames(mydata))]
答案 3 :(得分:2)
两个tidyverse选项;哪个更实用取决于实际数据的维度。两者都有条件地利用coalesce
。
手动:
library(tidyverse)
mydata %>% rowwise() %>% # group by row
mutate_at(vars(starts_with('A')), # for A prefixes, coalesce if not all NA
funs(ifelse(all(is.na(c(A1, A2, A3))), ., coalesce(., 0)))) %>%
mutate_at(vars(starts_with('B')), # likewise for B
funs(ifelse(all(is.na(c(B1, B2, B3))), ., coalesce(., 0))))
## Source: local data frame [8 x 7]
## Groups: <by row>
##
## # A tibble: 8 × 7
## ID A1 A2 A3 B1 B2 B3
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1 1 1 1 1 1 1
## 2 2 NA NA NA 1 1 1
## 3 3 1 0 0 1 1 0
## 4 4 0 1 0 1 1 0
## 5 5 1 0 1 1 0 1
## 6 6 1 1 0 1 1 0
## 7 7 1 0 0 NA NA NA
## 8 8 NA NA NA 1 0 0
或以编程方式,通过重塑:
mydata %>% gather(var, val, -ID) %>% # reshape to long
group_by(ID, letter = substr(var, 1, 1)) %>% # group by ID and prefix
mutate(val = if(all(is.na(val))) val else coalesce(val, 0)) %>%
ungroup() %>% select(-letter) %>% spread(var, val) # clean up
## # A tibble: 8 × 7
## ID A1 A2 A3 B1 B2 B3
## * <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1 1 1 1 1 1 1
## 2 2 NA NA NA 1 1 1
## 3 3 1 0 0 1 1 0
## 4 4 0 1 0 1 1 0
## 5 5 1 0 1 1 0 1
## 6 6 1 1 0 1 1 0
## 7 7 1 0 0 NA NA NA
## 8 8 NA NA NA 1 0 0
如果前缀可以超出单个字母,请将substr
替换为合适的正则表达式,例如sub('\\d+$', '', var)
。