R $ operator is invalid for atomic vectors -
i have dataset 1 of columns "#" sign. used following code remove column.
ia <- as.data.frame(sapply(ia,gsub,pattern="#",replacement=""))
however, after operation, 1 of integer column had changed factor.
i wonder happened , how can avoid that. appreciate it.
a more correct version of code might 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 2 2 b 3 3 4 4 c 5 5 d
but you'll note, approach never remove offending column. it's replacing #
values empty character strings. remove column of #
might 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 2 2 b 3 3 # 4 4 c 5 5 d
Comments
Post a Comment