R有条件地申请? [英] R conditional lapply?
问题描述
我有一个数据框,例如
a = c(2,NA,3,4)
b = c(NA,3,NA,NA)
c= c(5,NA,7,9)
test = data.frame(a,b,c)
> test
a b c
1 2 NA 5
2 NA 3 NA
3 3 NA 7
4 4 NA 9
我只想填写test $ b中的NA值与该行的test $ a和test $ c的平均值。结果应为
I would like to fill in only NA values in test$b with the average of test$a and test$c for that row. The result should be
a b c
1 2 3.5 5
2 NA 3 NA
3 3 5 7
4 4 6.5 9
我已经尝试过申请家庭,但还没有到任何地方。想要避免for循环,因为有人告诉我应该尽量避免for循环。
I have tried the apply family but haven't gotten anywhere. Would like to avoid a for loop because I am told I should try to avoid for loops.
我要说的是英语
if test$b[i] == NA, test$b[i] = (test$a[i] + test$b[i])/2
else leave test$b[i] as it is.
我确定这类问题已经回答了很多次,但是我找不到(或者识别)类似的东西。预先感谢。
I'm sure this kind of question has been answered many times but I can't find (or recognise) something analogous. Thanks in advance.
推荐答案
您可以为元素 NA中的元素创建逻辑行索引(indx) b列。通过使用除 b以外的列的 rowMeans,用该值替换 b中的NA值。 (根据@thelatemail的评论进行修改)
You can create a logical row index ('indx') for the elements that are 'NA' in the 'b' column. Use that to replace the NA values in 'b' by taking the `rowMeans of the columns other than 'b'. (Modified based on comments from @thelatemail)
indx <- is.na(test$b)
test$b[indx] <- rowMeans(test[indx,], na.rm=TRUE)
test
# a b c
#1 2 3.5 5
#2 NA 3.0 NA
#3 3 5.0 7
#4 4 6.5 9
这篇关于R有条件地申请?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!