3

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.

Rosalin August
  • 101
  • 1
  • 4

2 Answers2

5

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)])
1

Here it is:

library(dplyr)

col_index <- seq(1:ncol(example)) 
example %>% select(col_index[col_index %% 3 != 0]) 
denisafonin
  • 1,116
  • 1
  • 7
  • 16