简体   繁体   中英

Deleting every nth column from a dataframe in r

I am trying to reduce the size of a dataframe by removing every third column.

Here is my example dataframe:

example = data.frame(x=c(1,2,3,4), y=c(1,2,3,4), z=c(1,2,3,4), w=c(1,2,3,4), p=c(1,2,3,4), q=c(1,2,3,4), r=c(1,2,3,4))

Which looks like this

x y z w p q r
1 1 1 1 1 1 1
2 2 2 2 2 2 2
3 3 3 3 3 3 3
4 4 4 4 4 4 4

I would like to convert it into something that looks like this

x y w p r
1 1 1 1 1 
2 2 2 2 2 
3 3 3 3 3 
4 4 4 4 4 

I have been able to reduce the number of rows using tidyverse:

example <- example %>% dplyr::filter(row_number() %% 3 != 1) 

But I can't figure out how to delete every third column.

I have also tried to use this line:

example[, !(c%%3==0)]

from Deleting every n-th row in a dataframe but I keep getting this error: Error in c%%3: non-numeric argument to binary operator

Thanks in advance for your help.

Here it is:

library(dplyr)

col_index <- seq(1:ncol(example)) 
example %>% select(col_index[col_index %% 3 != 0]) 

You can do this in a very simple way in base.

example[, c(TRUE, TRUE, FALSE)]

The logical vector will repeat as needed for the columns. If you want it to scale, you can do something like this.

n <- 3
example[, c(rep(TRUE, n - 1), FALSE)]

If you prefer, the dplyr equivalent of this can be:

example %>%
  select(everything()[c(TRUE, TRUE, FALSE)])

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