2016-08-02 35 views
1

我有2个csv文件,其中的数据看起来像运行相同的R文件,我有数百行和列运行,我希望转换1 =注册,0 =退出“” =无数据:在多列R中转换值

Test <- read.csv("..\\..\TestFile.csv", colClasses = "factor") 
[1] [2] [3] 
1 0    
0 1 
1 1 
1 0 1 

[1] [2] [3] 
1 0    
1 1 
1 0 
1 0 1 

So, far I had tried on 

revalue(Test$1, c("1" = "Enroll")) -> Test$1 
revalue(Test$1, c("0" = "Quit")) -> Test$1 
revalue(Test$2, c("1" = "Enroll")) -> Test$2 
revalue(Test$2, c("0" = "Quit")) -> Test$2 
revalue(Test$3, c("1" = "Enroll")) -> Test$3 
revalue(Test$3, c("0" = "Quit")) -> Test$3 

write.csv(Test, "TestFile.csv", na = "No data") 

But, it promt the warning msg: The following `from` values were not present in `x`: 1. 
The NA string unable to update to "No data". Please help. 

回答

0

我们可以使用lapply改变各支柱

Test[] <- lapply(Test, function(x) {x1 <- c("Quit", "Enroll")[x+1] 
        replace(x1, is.na(x1), "No Data")}) 
Test 
# Col1 Col2 Col3 
#1 Enroll Quit Enroll 
#2 Quit Quit No Data 
#3 Quit Quit Quit 
#4 Enroll Enroll No Data 

的值如果我们有几百列,另一种方法是mutate_eachdplyr

library(dplyr) 
library(magrittr) 
Test %<>% 
     mutate_each(funs(c("Quit", "Enroll")[.+1])) %<>% 
     mutate_each(funs(replace(., is.na(.), "No Data"))) 
+0

我不得不尝试测试[] < - lapply(试验,函数(X)ifelse(X == 0, “注册”, “退出”)),坚果我的NA值成为退出,而不是我想要的NA值替换为无数据 – wow5

+1

@akrun对我来说一个新事物是'%<>%',让我检查它的功能。谢谢。 –

+1

@ SowmyaS.Manian它会改变'Test'中的值。因此,我们不需要将其分配给'Test <- Test %>%...' – akrun

1

一,创建数据帧df10NA

df <- data.frame(Col1 = c(1,0,0,1), Col2 = c(0,0,0,1), Col3 = c(1,NA,0,NA)) 
    df 
    # Col1 Col2 Col3 
    # 1 1 0 1 
    # 2 0 0 NA 
    # 3 0 0 0 
    # 4 1 1 NA 

II。替换具体数值

df[df == 0] <- "Quit" 
    df[df == 1] <- "Enroll" 
    df[is.na(df)] <- "No Data" 

三,最终df

df 
    #  Col1 Col2 Col3 
    # 1 Enroll Quit Enroll 
    # 2 Quit Quit No Data 
    # 3 Quit Quit Quit 
    # 4 Enroll Enroll No Data 

OR

​​
+0

当我替换特定的值时,它会提示警告消息 在'[< - 。factor'('* tmp *',thisvar,值=“退出”):无效因子水平,产生的不适用 – wow5

+0

请向您显示您从中得到此错误的实际命令。可能那么它会更好理解。 –