I am trying to remove data frame columns by subsetting the dataframe with a list of varnames, but I am getting an error:
stones<-grep("^stat\\.mineBlock\\.minecraft\\.[a-zA-Z0-9]*?stone[a-zA-Z0-9]*?", colnames(ingame), value=TRUE, ignore.case=T, perl=T)
stones<-lapply(stones, function(x) get(x))
out<-subset(out,select=-c(stones))
stones values after running line 1 above:
> stones
[1] "stat.mineBlock.minecraft.sandstone" "stat.mineBlock.minecraft.stone" "stat.mineBlock.minecraft.cobblestone" "stat.mineBlock.minecraft.cobblestone_wall"
[5] "stat.mineBlock.minecraft.stone_slab" "stat.mineBlock.minecraft.redstone_ore" "stat.mineBlock.minecraft.stone_stairs" "stat.mineBlock.minecraft.glowstone"
Error on line 3:
> out<-subset(out,select=-c(stones))
Error in -c(stones) : invalid argument to unary operator
Using the unary operator -
on character values is not allowed.
But you could use the logical negation of %in%
out[, !names(out) %in% stones]
You may even be able to use !names(out) %in% stones
as the argument to select
but I'm not certain due to a lack of reproducible example. For example, this works:
subset(mtcars, select = !names(mtcars) %in% "mpg")
but it's usually much safer to go with [
subsetting.
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.