DATA = data.table(col_1 = factor(c("A", "B", "C", "C", "B", "A", "C")),
col_2 = factor(c("stuff", NA, NA, "stuff", NA, "different_stuff", NA)))
I have a big data set in which I would like to replace the NAs
from col2
, that correspond to C
from col1
, with a new factor level, eg yet_another_stuff
. There are more NAs
than there are observations with C
level and I don't want to replace the NAs
that belong to other level like B
.
After uploading this data set the columns are already of class factor.
I would highly prefer to do so using data.table
package due to the size of the data set.
我们可以在i
指定逻辑条件,并在'col_2'中分配与'yet_another_stuff'条件对应的那些值
DATA[is.na(col_2) & col_1 == "C", col_2 := "yet_another_stuff"]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.