根据上一列添加NA

时间:2018-12-07 14:19:59

标签: r

我的数据框如下:

Total   Taxes   VAT Debit   Credit  Department  Personal    Car
17.5    0.5     no    x       NA       yes         no       yes
72.5    0.5     no    x       NA       yes         yes      no
1.4      NA     no    x       NA       yes         no       yes
4.5      NA     no    x       NA       yes         no       yes
1.5      NA     no    x       NA       yes         no       yes

,我希望在departmentpersonalcar中,我的输出改为显示NANA基于taxes列。例如,如果NA中有一个taxes,则在下面的列中应该有一个NA,如下例所示:

Total   Taxes   VAT Debit   Credit  Department  Personal    Car
17.5     0.5    no    x      NA         yes        no       yes
72.5     0.5    no    x      NA         yes        yes      no
1.4      NA     no    NA     NA         NA         NA       NA
4.5      NA     no    NA     NA         NA         NA       NA
1.5      NA     no    NA     NA         NA         NA       NA

BaseR中是否有解决方案?

2 个答案:

答案 0 :(得分:2)

mstr = 'ABCDZ' print(rotate_str(mstr, 3)) #=> CDZAB print(rotate_str(mstr, -4)) #=> ZABCD 的方式是

dplyr

答案 1 :(得分:1)

基于“税收”创建索引,并将感兴趣的列分配给NA中的base R

i1 <- is.na(df1$Taxes)
colsOfInterest <- c('Debit', 'Department', 'Personal', 'Car')
df1[i1, colsOfInterest] <- NA
df1
#  Total Taxes VAT Debit Credit Department Personal  Car
#1  17.5   0.5  no     x     NA        yes       no  yes
#2  72.5   0.5  no     x     NA        yes      yes   no
#3   1.4    NA  no  <NA>     NA       <NA>     <NA> <NA>
#4   4.5    NA  no  <NA>     NA       <NA>     <NA> <NA>
#5   1.5    NA  no  <NA>     NA       <NA>     <NA> <NA>