使用R将不正确的值移到右列

cgyqldqp  于 2023-11-14  发布在  其他
关注(0)|答案(3)|浏览(121)

我有一个名为Brand_ID的列,其中ID的范围应该是0-6,但有一些User_ID编号被错误地记录在我的Brand_ID列中。我一直无法找到将这些编号移动到正确列的方法。
以下是数据的示例:

Brand_ID             User_ID
1060                   0 1515915625212799232
1061                   1 1515915625193362688
1062                   0 1515915625193362688
1063 1515915625216376320                  NA
1064 1515915625216376320                  NA
1065 1515915625216376320                  NA
1066 1515915625216376320                  NA
1067                   1 1313987370474800128
1068                   0 1313987370474800128
1069                   0 1515915625193362688
1070                   0 1515915625061714688

字符串
dput()

structure(list(Brand_ID = c(0, 1, 0, 1515915625216376320, 1515915625216376320, 
1515915625216376320, 1515915625216376320, 1, 0, 0, 0), User_ID = c(1515915625212799232, 
1515915625193362688, 1515915625193362688, NA, NA, NA, NA, 1313987370474800128, 
1313987370474800128, 1515915625193362688, 1515915625061714688
)), row.names = 1060:1070, class = "data.frame")


我已经尝试了下面的代码,只是检查我能够循环通过Brand_ID列中的User_ID值,但没有打印。我也尝试使用返回和粘贴而不是打印,但没有成功。
我还没有达到真正尝试转换值的地步,但我也不知道如何做到这一点。
任何帮助是赞赏!

brand <- !is.na(j$Brand_ID)
> for (x in 1:length(brand)){
+   if (brand[x] > 6)
+     {print(x)}
+   else
+     {next}
+ }

c2e8gylq

c2e8gylq1#

这样的东西应该可以工作。我也得到了科学计数法,因此选项(scipen = 999)。

options(scipen = 999)
library(dplyr)
brand_fixed <- brand %>%
  mutate(User_ID = case_when(Brand_ID > 6 ~ Brand_ID,
                             Brand_ID <= 6 ~ User_ID)) %>%
           mutate(Brand_ID = case_when(Brand_ID > 6 ~ NA,
                                       Brand_ID <= 6 ~ Brand_ID))

字符串
其思想是mutate通过使用case_when来评估是插入Brand_ID的当前值还是使用已经存在的User_ID来覆盖每个列。
希望这能帮上忙。

bwleehnv

bwleehnv2#

另一种基于R的方法,d是原始数据:

z <- is.na(d$User_ID)
    d[z, ] <- d[z, c('User_ID', 'Brand_ID')]
## > d
    ##      Brand_ID      User_ID
    ## 1060        0 1.515916e+18
    ## 1061        1 1.515916e+18
    ## 1062        0 1.515916e+18
    ## 1063       NA 1.515916e+18
    ## 1064       NA 1.515916e+18
    ## 1065       NA 1.515916e+18
    ## 1066       NA 1.515916e+18
    ## 1067        1 1.313987e+18
    ## 1068        0 1.313987e+18
    ## 1069        0 1.515916e+18
    ## 1070        0 1.515916e+18
qlckcl4x

qlckcl4x3#

一个R基的方法:

options(scipen = 999)
df[!df$Brand_ID %in% 0L:6L, ]$User_ID <- df[!df$Brand_ID %in% 0L:6L, ]$Brand_ID
df[!df$Brand_ID %in% 0L:6L, ]$Brand_ID <- NA

字符串

相关问题