Having a data frame, how do I go about replacing all particular values along all rows and columns. Say for example I want to replace all empty records with NA
's (without typing the positions):
有了数据框架,我如何替换所有行和列上的所有特定值。例如,我想用NA替换所有空记录(不输入位置):
df <- data.frame(list(A=c("", "xyz", "jkl"), B=c(12, "", 100))) A B1 122 xyz 3 jkl 100
Expected result:
预期结果:
A B1 NA 122 xyz NA 3 jkl 100
4 个解决方案
#1
77
Like this:
是这样的:
> df[df==""]<-NA> df A B1 <NA> 122 xyz <NA>3 jkl 100
#2
19
Since PikkuKatja and glallen asked for a more general solution and I cannot comment yet, I'll write an answer. You can combine statements as in:
由于pikkkukatja和glallen要求一个更一般的解决方案,我现在还不能评论,我将写一个答案。您可以将语句合并为:
> df[df=="" | df==12] <- NA> df A B1 <NA> <NA>2 xyz <NA>3 jkl 100
For factors, zxzak's code already yields factors:
对于因子,zxzak的代码已经产生因子:
> df <- data.frame(list(A=c("","xyz","jkl"), B=c(12,"",100)))> str(df)'data.frame': 3 obs. of 2 variables: $ A: Factor w/ 3 levels "","jkl","xyz": 1 3 2 $ B: Factor w/ 3 levels "","100","12": 3 1 2
If in trouble, I'd suggest to temporarily drop the factors.
如果遇到麻烦,我建议暂时放弃这些因素。
df[] <- lapply(df, as.character)
#3
3
We can use data.table to get it quickly.First create df without factors,
我们可以使用的数据。桌子可以让它快速移动。首先创建无因子df,
df <- data.frame(list(A=c("","xyz","jkl"), B=c(12,"",100)), stringsAsFactors=F)
Now you can use
现在你可以使用
setDT(df)for (jj in 1:ncol(df)) set(df, i = which(df[[jj]]==""), j = jj, v = NA)
and you can convert it back to a data.frame
你可以把它转换回data.frame
setDF(df)
If you only want to use data.frame and keep factors it's more difficult, you need to work with
如果你只想用data.frame且保存因子会更困难,那你就需要使用它
levels(df$value)[levels(df$value)==""] <- NA
where value is the name of every column. You need to insert it in a loop.
其中value是每个列的名称。您需要将它插入到循环中。
#4
0
If you want to replace multiple values in a data frame, looping through all columns might help.
如果您想要在数据帧中替换多个值,那么遍历所有列可能会有所帮助。
Say you want to replace ""
and 100
:
假设你想要替换“”和100:
na_codes <- c(100, "")for (i in seq_along(df)) { df[[i]][df[[i]] %in% na_codes] <- NA}
#1
77
Like this:
是这样的:
> df[df==""]<-NA> df A B1 <NA> 122 xyz <NA>3 jkl 100
#2
19
Since PikkuKatja and glallen asked for a more general solution and I cannot comment yet, I'll write an answer. You can combine statements as in:
由于pikkkukatja和glallen要求一个更一般的解决方案,我现在还不能评论,我将写一个答案。您可以将语句合并为:
> df[df=="" | df==12] <- NA> df A B1 <NA> <NA>2 xyz <NA>3 jkl 100
For factors, zxzak's code already yields factors:
对于因子,zxzak的代码已经产生因子:
> df <- data.frame(list(A=c("","xyz","jkl"), B=c(12,"",100)))> str(df)'data.frame': 3 obs. of 2 variables: $ A: Factor w/ 3 levels "","jkl","xyz": 1 3 2 $ B: Factor w/ 3 levels "","100","12": 3 1 2
If in trouble, I'd suggest to temporarily drop the factors.
如果遇到麻烦,我建议暂时放弃这些因素。
df[] <- lapply(df, as.character)
#3
3
We can use data.table to get it quickly.First create df without factors,
我们可以使用的数据。桌子可以让它快速移动。首先创建无因子df,
df <- data.frame(list(A=c("","xyz","jkl"), B=c(12,"",100)), stringsAsFactors=F)
Now you can use
现在你可以使用
setDT(df)for (jj in 1:ncol(df)) set(df, i = which(df[[jj]]==""), j = jj, v = NA)
and you can convert it back to a data.frame
你可以把它转换回data.frame
setDF(df)
If you only want to use data.frame and keep factors it's more difficult, you need to work with
如果你只想用data.frame且保存因子会更困难,那你就需要使用它
levels(df$value)[levels(df$value)==""] <- NA
where value is the name of every column. You need to insert it in a loop.
其中value是每个列的名称。您需要将它插入到循环中。
#4
0
If you want to replace multiple values in a data frame, looping through all columns might help.
如果您想要在数据帧中替换多个值,那么遍历所有列可能会有所帮助。
Say you want to replace ""
and 100
:
假设你想要替换“”和100:
na_codes <- c(100, "")for (i in seq_along(df)) { df[[i]][df[[i]] %in% na_codes] <- NA}