如何将数据框中的所有非数字单元格转换为 NA
How to convert all non numeric cells in data frame to NA
我正在尝试将所有具有非数值的单元格转换为缺失数据 (NA)。我尝试了类似的方法,将特定值转换为缺失数据,例如:
recode_missing <- function (g, misval)
{
a <- g == misval
temp = g
temp [a] <- NA
return (temp)
}
效果很好:一个优雅的 R 解决方案。
我尝试像 a <- g == is.numeric ()
(语法错误)、a <- is.numeric (g): (Error: (list) object cannot be coerced to type 'double'), or even
a [] <- is.numeric (g[]`(相同)那样解码。我知道删除列的解决方案
remove_nn <- function (data)
{
# removes all non-numeric columns
numeric_columns <- sapply (data, is.numeric)
return (data [, numeric_columns])
} ### remove_nn ###
但这会删除列并将数据框转换为某个矩阵。
有人可以建议如何在保持数据结构不变的情况下将单个非数字单元格转换为 NA 吗?
编辑
正如评论正确指出的那样,在数值的海洋中没有单独的字符串值这样的东西。只是数字或其他东西的向量。我现在想知道是什么导致了 medians <- apply (data, 2, median)
中的非数字错误。我有很多载体,但用肉眼检查证明是无用的。我发布了 num <- sapply (data, is.numeric)
和下一个 data [,!num]
。这给了我非数字的列。在一种情况下,这是由 one 单元格值包含多余的 " 引起的。该文件由电子表格预处理,如果只有一个单元格是非数字的,则完整的矢量被视为非数字。
根据您的编辑,您的向量应该是数字,但由于在读入过程中引入了一些错误数据,数据已转换为另一种格式(可能 character
或 factor
).
这是一个例子。 mydf1 <- mydf2 <- mydf3 <-
data.frame(...)
只是用相同的数据创建了三个 data.frame
。
# I'm going to show three approaches
mydf1 <- mydf2 <- mydf3 <- data.frame(
A = c(1, 2, "x", 4),
B = c("y", 3, 4, "-")
)
str(mydf1)
# 'data.frame': 4 obs. of 2 variables:
# $ A: Factor w/ 4 levels "1","2","4","x": 1 2 4 3
# $ B: Factor w/ 4 levels "-","3","4","y": 4 2 3 1
一种方法是让 R 将任何不能转换为数字的值强制转换为 NA
:
## You WILL get warnings
mydf1[] <- lapply(mydf1, function(x) as.numeric(as.character(x)))
# Warning messages:
# 1: In FUN(X[[i]], ...) : NAs introduced by coercion
# 2: In FUN(X[[i]], ...) : NAs introduced by coercion
str(mydf1)
# 'data.frame': 4 obs. of 2 variables:
# $ A: num 1 2 NA 4
# $ B: num NA 3 4 NA
另一种选择是使用 my SOfun package:
中的 makemeNA
library(SOfun)
makemeNA(mydf2, "[^0-9]", FALSE)
# A B
# 1 1 NA
# 2 2 3
# 3 NA 4
# 4 4 NA
str(.Last.value)
# 'data.frame': 4 obs. of 2 variables:
# $ A: int 1 2 NA 4
# $ B: int NA 3 4 NA
这个函数有点不同,它使用 type.convert
进行转换,并且可以处理更具体的规则以转换为 NA
(就像您可以使用向量 na.strings
将数据读入 R).
关于你的错误,我相信你会在你的 data.frame
上尝试 as.numeric
来得到你所显示的错误。
示例:
# Your error...
as.numeric(mydf3)
# Error: (list) object cannot be coerced to type 'double'
你不会在 matrix
上得到那个错误(但你仍然会得到警告)....
# You'll get a warning
as.numeric(as.matrix(mydf3))
# [1] 1 2 NA 4 NA 3 4 NA
# Warning message:
# NAs introduced by coercion
为什么我们不需要显式使用 as.character
? as.matrix
为您做到这一点:
str(as.matrix(mydf3))
# chr [1:4, 1:2] "1" "2" "x" "4" "y" "3" "4" "-"
# - attr(*, "dimnames")=List of 2
# ..$ : NULL
# ..$ : chr [1:2] "A" "B"
如何使用这些信息?
mydf3[] <- as.numeric(as.matrix(mydf3))
# Warning message:
# NAs introduced by coercion
str(mydf3)
# 'data.frame': 4 obs. of 2 variables:
# $ A: num 1 2 NA 4
# $ B: num NA 3 4 NA
简单是最好的。选择列 - 我选择了第 4 列到第 31 列。
df[,4:31] <- as.numeric(as.factor(as.character(df[,4:31])))
我正在尝试将所有具有非数值的单元格转换为缺失数据 (NA)。我尝试了类似的方法,将特定值转换为缺失数据,例如:
recode_missing <- function (g, misval)
{
a <- g == misval
temp = g
temp [a] <- NA
return (temp)
}
效果很好:一个优雅的 R 解决方案。
我尝试像 a <- g == is.numeric ()
(语法错误)、a <- is.numeric (g): (Error: (list) object cannot be coerced to type 'double'), or even
a [] <- is.numeric (g[]`(相同)那样解码。我知道删除列的解决方案
remove_nn <- function (data)
{
# removes all non-numeric columns
numeric_columns <- sapply (data, is.numeric)
return (data [, numeric_columns])
} ### remove_nn ###
但这会删除列并将数据框转换为某个矩阵。
有人可以建议如何在保持数据结构不变的情况下将单个非数字单元格转换为 NA 吗?
编辑
正如评论正确指出的那样,在数值的海洋中没有单独的字符串值这样的东西。只是数字或其他东西的向量。我现在想知道是什么导致了 medians <- apply (data, 2, median)
中的非数字错误。我有很多载体,但用肉眼检查证明是无用的。我发布了 num <- sapply (data, is.numeric)
和下一个 data [,!num]
。这给了我非数字的列。在一种情况下,这是由 one 单元格值包含多余的 " 引起的。该文件由电子表格预处理,如果只有一个单元格是非数字的,则完整的矢量被视为非数字。
根据您的编辑,您的向量应该是数字,但由于在读入过程中引入了一些错误数据,数据已转换为另一种格式(可能 character
或 factor
).
这是一个例子。 mydf1 <- mydf2 <- mydf3 <-
data.frame(...)
只是用相同的数据创建了三个 data.frame
。
# I'm going to show three approaches
mydf1 <- mydf2 <- mydf3 <- data.frame(
A = c(1, 2, "x", 4),
B = c("y", 3, 4, "-")
)
str(mydf1)
# 'data.frame': 4 obs. of 2 variables:
# $ A: Factor w/ 4 levels "1","2","4","x": 1 2 4 3
# $ B: Factor w/ 4 levels "-","3","4","y": 4 2 3 1
一种方法是让 R 将任何不能转换为数字的值强制转换为 NA
:
## You WILL get warnings
mydf1[] <- lapply(mydf1, function(x) as.numeric(as.character(x)))
# Warning messages:
# 1: In FUN(X[[i]], ...) : NAs introduced by coercion
# 2: In FUN(X[[i]], ...) : NAs introduced by coercion
str(mydf1)
# 'data.frame': 4 obs. of 2 variables:
# $ A: num 1 2 NA 4
# $ B: num NA 3 4 NA
另一种选择是使用 my SOfun package:
中的makemeNA
library(SOfun)
makemeNA(mydf2, "[^0-9]", FALSE)
# A B
# 1 1 NA
# 2 2 3
# 3 NA 4
# 4 4 NA
str(.Last.value)
# 'data.frame': 4 obs. of 2 variables:
# $ A: int 1 2 NA 4
# $ B: int NA 3 4 NA
这个函数有点不同,它使用 type.convert
进行转换,并且可以处理更具体的规则以转换为 NA
(就像您可以使用向量 na.strings
将数据读入 R).
关于你的错误,我相信你会在你的 data.frame
上尝试 as.numeric
来得到你所显示的错误。
示例:
# Your error...
as.numeric(mydf3)
# Error: (list) object cannot be coerced to type 'double'
你不会在 matrix
上得到那个错误(但你仍然会得到警告)....
# You'll get a warning
as.numeric(as.matrix(mydf3))
# [1] 1 2 NA 4 NA 3 4 NA
# Warning message:
# NAs introduced by coercion
为什么我们不需要显式使用 as.character
? as.matrix
为您做到这一点:
str(as.matrix(mydf3))
# chr [1:4, 1:2] "1" "2" "x" "4" "y" "3" "4" "-"
# - attr(*, "dimnames")=List of 2
# ..$ : NULL
# ..$ : chr [1:2] "A" "B"
如何使用这些信息?
mydf3[] <- as.numeric(as.matrix(mydf3))
# Warning message:
# NAs introduced by coercion
str(mydf3)
# 'data.frame': 4 obs. of 2 variables:
# $ A: num 1 2 NA 4
# $ B: num NA 3 4 NA
简单是最好的。选择列 - 我选择了第 4 列到第 31 列。
df[,4:31] <- as.numeric(as.factor(as.character(df[,4:31])))