Replace missing values with a value from another column

后端 未结 2 1573
故里飘歌
故里飘歌 2020-12-10 09:13

If I have:

s <- data.frame(ID=c(191, 282, 202, 210), Group=c(\"\", \"A\", \"\", \"B\"), stringsAsFactors=FALSE)
s
   ID Group
1 191      
2 282     A
3 20         


        
2条回答
  •  隐瞒了意图╮
    2020-12-10 10:02

    We can use data.table to assign the values in "Group2" to "Group" where the "Group" is "" specified in the "i" condition.

    library(data.table)
    setDT(s)[Group=="", Group:= Group2]
    

    As the assignment happens in place, it is considered to be efficient.

提交回复
热议问题