[英]Convert NA values in a specific column to "" field in R
I have a dataset, df that I reading in from Excel to R. For some reason, when reading in the file, R sets all the empty fields to NA.我有一个数据集 df,我从 Excel 读取到 R。出于某种原因,在读取文件时,R 将所有空字段设置为 NA。 How do I reverse this?
我如何扭转这种情况? I want the NA values in the column to be converted back to empty cells.
我希望将列中的 NA 值转换回空单元格。
Subject Value
hello NA
hello NA
hello NA
I would like:我想:
Subject Value
hello
hello
hello
Here is the dput:这是dput:
structure(list(Subject = structure(c(1L, 1L, 1L), .Label = "hello", class = "factor"),
Value = c(NA, NA, NA)), class = "data.frame", row.names = c(NA,
-3L))
This is what I have tried:这是我尝试过的:
df[is.na(df$Value)] <- " "
. .
I do not know if this structure is correct Any help is appreciated.我不知道这个结构是否正确任何帮助表示赞赏。
We need to assign the same column name我们需要分配相同的列名
df$Value[is.na(df$Value)] <- ""
Instead, if we do the subset on the whole dataset, it would result in error相反,如果我们在整个数据集上做子集,就会导致错误
df1[is.na(df1$Value)]
Error in
[.data.frame
(df1, is.na(df1$Value)) : undefined columns selected[.data.frame
(df1, is.na(df1$Value)) 中的错误:选择了未定义的列
With tidyverse
, we can also use replace_na
使用
tidyverse
,我们也可以使用replace_na
library(dplyr)
library(tidyr)
df1 <- df1 %>%
mutate(Value = replace_na(Value, ""))
df1
# Subject Value
#1 hello
#2 hello
#3 hello
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.