简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM