用同一数据框中另一列的值替换单元格的NA值 [英] Replace the NA value of a cell by the value of another column in the same dataframe

查看:116
本文介绍了用同一数据框中另一列的值替换单元格的NA值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来很简单的问题,但我无法自己解决.我已经在StackOverflow上搜索了解决方案,我猜它已经被某个人解决了,但是我还没有找到它.

I have a problem which seems to me quite simple but I don't manage to solve it by myself. I've searched for the solution on StackOverflow, I guess it has already been solved by someone but I haven't found it yet.

我有一个基于5个数据帧合并的数据帧,看起来像这样:

I have a data frame based upon the merger of 5 data frames, which looks like that :

id | mag1 | mag2 | mag3
1 | name | name | name
2 | NA | NA | name
3 | NA | name | NA

对于mag2和mag3,总是会填充一个名称(在mag1,mag2和mag3中没有带有NA的行).我想更改mag1的值,以使其永远不会为空,并采用下一个非空单元格的值.

With mag2 and mag3 there always is a name which is filled (there is no row with an NA in mag1, mag2 and mag3). I would like to change the value of mag1 in order that it is never empty and that it takes the value of the next non-empty cell.

我曾想过使用这种代码:

I have imagined to use this kind of code :

db$mag1[is.na(db$mag1)] <- db$mag2
db$mag1[is.na(db$mag1)] <- db$mag3

使用此代码,在我看来,例如,在第二行中,用db $ mag2的值替换将使mag1保持不变(NA),而用db $ mag3的替换将其值更改为姓名".如果mag2中存在非NA值,则不应激活第二行.

With this code, it seems to me that for instance, in the second line, the replacement with the value of db$mag2 will leave mag1 unchanged (NA) and that the replacement with db$mag3 will change its value to "name". The second line shouldn't be activated if there is a non-NA value in mag2.

现在,这是我遇到的错误:

Now, here is the error I got :

Warning message:
In db$mag[is.na(db$mag1)] <- db$mag2 :
   number of items to replace is not a multiple of replacement length

我猜我的代码行中有一个非常简单的错误,但我看不到它.有什么主意吗?

I guess there is a very simple error in my code line, but I don't manage to see it. Any idea?

推荐答案

您必须在分配<-的两侧都使用逻辑索引,以使lengths相同,并替换相应的元素.

You have to use the logical index on both sides of the assignment <- so that the lengths are the same and corresponding elements are replaced.

 db$mag1[is.na(db$mag1)] <- db$mag3[is.na(db$mag1)]
 db
 #  id mag1 mag2 mag3
 #1  1 name name name
 #2  2 name <NA> name
 #3  3 <NA> name <NA>

数据

 db <- structure(list(id = 1:3, mag1 = c("name", NA, NA), mag2 = c("name", 
 NA, "name"), mag3 = c("name", "name", NA)), .Names = c("id", 
 "mag1", "mag2", "mag3"), class = "data.frame", row.names = c(NA, 
 -3L))

这篇关于用同一数据框中另一列的值替换单元格的NA值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆