r - 将特定列中的 NA 值转换为 R 中的“”字段
问题描述
我有一个数据集 df,我从 Excel 读取到 R。出于某种原因,在读取文件时,R 将所有空字段设置为 NA。我该如何扭转这个?我希望将列中的 NA 值转换回空单元格。
Subject Value
hello NA
hello NA
hello NA
我想:
Subject Value
hello
hello
hello
这是输出:
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))
这是我尝试过的:
df[is.na(df$Value)] <- " "
.
我不知道这种结构是否正确 任何帮助表示赞赏。
解决方案
我们需要分配相同的列名
df$Value[is.na(df$Value)] <- ""
相反,如果我们对整个数据集执行子集,则会导致错误
df1[is.na(df1$Value)]
[.data.frame
(df1, is.na(df1$Value))中的错误:选择了未定义的列
tidyverse
,我们也可以使用replace_na
library(dplyr)
library(tidyr)
df1 <- df1 %>%
mutate(Value = replace_na(Value, ""))
df1
# Subject Value
#1 hello
#2 hello
#3 hello
推荐阅读
- javascript - 为字符串的特定部分着色
- google-cloud-functions - Stackdriver Logging 未正确处理 Google Cloud Function 中的 python 退出(代码)堆栈跟踪,还有其他选择吗?
- python - 如何在 Python 的末尾发出声音?
- c++ - Debian gcc 不良行为
- jsf - 使用提交按钮调用 bean-method 并处理方法中文本字段的输入
- elasticsearch - 可以列出丢失数据的 UniqueID 的 Elasticsearch 查询
- c# - 在 EF Core 中自动填充 Created 和 LastModified
- algorithm - 证明 ak 级流水线最多可以比非流水线的快 k 倍
- c# - 我尝试使用 webBrowser1 更改链接
- php - 允许用户代表我从 Google 云端硬盘下载文件