简体   繁体   中英

How to remove columns by name in R when numerical?

I have a dataset from a machine output and the columns are numerically named. I need to remove certain columns by name because I don't want to be dependent on the location of a range (like 42:67, it could be 40:60 in a different dataset). When I read in the CSV dataset I set check.names=FALSE in order to not have an x in front of every column. I do that because when I melt/gather the data I need the numerical aspect to sort and plot the data so I don't want to deal with the x.

Here is what I am trying that is not working...

#Listing the column names to cut from beginning
beg.noise <- seq(from = 285, to = 414, by = 3)

#Listing the column names to cut from ending
end.blank <- seq(from = 1134, to = 1182, by = 3)

#Merging lists
columns.to.cut <- c(beg.noise, end.blank)

#Method 1 
clean.data <- subset(sample.data, select= -columns.to.cut)

#Method 2 
clean.data <-sample.data[,-columns.to.cut]

#Method 3 not much different that 1st
clean.data <- dplyr::select(sample.data, -columns.to.cut)

Example data with 300 columns and 2 row observations

sample.data <- as.data.frame(matrix(ncol=300, nrow=3, byrow = TRUE, c(as.character(seq(from=285, to= 1182, by=3)), rnorm(300, mean=0, sd=1), rnorm(300, mean=0, sd=1))))

#Setting first row as column headers
colnames(sample.data) <- as.character(unlist(sample.data[1,]))
sample.data = sample.data[-1, ]

Even though they are numbers, your column names are of class character :

class(colnames(sample.data[1]))
[1] "character"

So a vector of class numeric will not match, even if they look the same. Just apply the function as.character to convert them from numeric to character :

beg.noise <- as.character(seq(from = 285, to = 414, by = 3))

You state that "when I melt/gather the data I need the numerical aspect to sort and plot the data". This suggests an alternative: leave the "X" in the column name and deal with it after you gather .

For example - to remove the range 2:3

library(dplyr)
sample_data <- data.frame(X1 = 1:5,
                          X2 = 6:10, 
                          X3 = 11:15, 
                          X4 = 16:20)

sample_data %>% 
  gather(variable, value) %>% 
  # remove the X and convert to numeric
  mutate(variable = gsub("X", "", variable), 
         variable = as.numeric(variable)) %>% 
         filter(!between(variable, 2, 3))

   variable value
1         1     1
2         1     2
3         1     3
4         1     4
5         1     5
6         4    16
7         4    17
8         4    18
9         4    19
10        4    20

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