r - R. 删除“#NAME?”时出现问题?(从 excel 导入)在数据框中
问题描述
我有一个从 excel 导入的 .csv 文件,其中包含我要删除的公式挂断。数据的简单版本如下。
library(tidyverse)
df <- data.frame(
species = letters[1:5],
param1 = c("Place", "creek", "river", "#VALUE!", "desert"),
param2 = c(-23.8, 43.23, "#NAME?", 45, 0.23),
param3 = c(2.4, 2, 5.7, 0.00003, -2.5),
stringsAsFactors = FALSE
) # This is a simplified version of the excel .csv import
df[df == "#VALUE!"] <- "" # Removes excel cells where the formula left "#VALUE!"
df[df == "#NAME\\?"] <- "" # This does not work
ndf <- df # This is an attempt to reassign the columns to numeric
ndf
class(ndf$param2)
class(ndf$param3)
主要的问题是,Param2
剩下的数据列在需要时分配给character
它numeric
,或者我必须在其上运行的功能不起作用。
我尝试了很多不同的东西,但是我似乎总是什么都认不出这个细胞。如何删除“#NAME”?请穿过df?
解决方案
您正在进行完全匹配(而不是正则表达式匹配),因此您不需要以不同的方式转义特殊变量(如?
, !
)。尝试 :
df[df == "#VALUE!"] <- ""
df[df == "#NAME?"] <- NA
df <- type.convert(df, as.is = TRUE)
df
# species param1 param2 param3
#1 a Place -23.80 2.40000
#2 b creek 43.23 2.00000
#3 c river NA 5.70000
#4 d 45.00 0.00003
#5 e desert 0.23 -2.50000
str(df)
#'data.frame': 5 obs. of 4 variables:
# $ species: chr "a" "b" "c" "d" ...
# $ param1 : chr "Place" "creek" "river" "" ...
# $ param2 : num -23.8 43.23 NA 45 0.23
# $ param3 : num 2.4 2 5.7 0.00003 -2.5
推荐阅读
- express - 本地数据中心未定义:cassandraDB 和 expressJS
- ios - 虚线在uiview中直到最后才出现
- javascript - 我是否必须指定映射才能在 Kuzzle 中使用 document.search 控制器?
- python - tensorflow.keras.models 遇到错误
- c# - C#如何从项目根目录下的文件夹中读取文件
- python - 有没有办法创建一个与变量同名的文件夹,然后在 python 中放置一个 JSON 文件?
- html - 有人可以帮我在这个 div 上对齐 X 吗?我一直在玩它,无法弄清楚:(
- java - 将字符串电话号码转换为长:数字格式异常
- python - strptime 不更改 Excel 中的日期格式
- ios - 如何使用按钮获取 collectionView 的 indexPathForVisableSections?