我有2个csv文件,其中数据看起来像运行相同的R文件,我有数百行和列运行,我想转换1 =注册,0 =退出,“”=没有数据:
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 :(得分:1)
予。使用df
,1
和0
NA
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"
III。最后df
df
# Col1 Col2 Col3
# 1 Enroll Quit Enroll
# 2 Quit Quit No Data
# 3 Quit Quit Quit
# 4 Enroll Enroll No Data
OR
c(df[is.na(df)] <- "No Data" , df[df == 0] <- "Quit", df[df == 1] <- "Enroll")
# [1] "No Data" "Quit" "Enroll"
df
# Col1 Col2 Col3
# 1 Enroll Quit Enroll
# 2 Quit Quit No Data
# 3 Quit Quit Quit
# 4 Enroll Enroll No Data
答案 1 :(得分: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_each
dplyr
library(dplyr)
library(magrittr)
Test %<>%
mutate_each(funs(c("Quit", "Enroll")[.+1])) %<>%
mutate_each(funs(replace(., is.na(.), "No Data")))