[英]R $ operator is invalid for atomic vectors
I have a dataset where one of the columns are only "#" sign. 我有一个数据集,其中一列只有“#”符号。 I used the following code to remove this column.
我使用以下代码删除此列。
ia <- as.data.frame(sapply(ia,gsub,pattern="#",replacement=""))
However, after this operation, one of the integer column I had changed to factor. 但是,在此操作之后,整数列中的一个已更改为factor。
I wonder what happened and how can i avoid that. 我想知道发生了什么,我怎么能避免这种情况。 Appreciate it.
欣赏它。
A more correct version of your code might be something like this: 更正确的代码版本可能是这样的:
d <- data.frame(x = as.character(1:5),y = c("a","b","#","c","d"))
> d[] <- lapply(d,gsub,pattern = "#",replace = "")
> d
x y
1 1 a
2 2 b
3 3
4 4 c
5 5 d
But as you'll note, this approach will never actually remove the offending column. 但正如您将注意到的,这种方法实际上永远不会删除违规列。 It's just replacing the
#
values with empty character strings. 它只是用空字符串替换
#
值。 To remove a column of all #
you might do something like this: 要删除所有
#
列,您可能会执行以下操作:
d <- data.frame(x = as.character(1:5),
y = c("a","b","#","c","d"),
z = rep("#",5))
> d[,!sapply(d,function(x) all(x == "#"))]
x y
1 1 a
2 2 b
3 3 #
4 4 c
5 5 d
Surely if you want to remove an offending column from a data frame, and you know which column it is, you can just subset. 当然,如果你想从数据框中删除一个有问题的列,并且你知道它是哪一列,你可以只是子集。 So, if it's the first column:
所以,如果它是第一列:
df <- df[,-1]
If it's a later column, increment up. 如果是后面的列,则递增。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.