Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting every nth column from a dataframe in r

Tags:

dataframe

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.

like image 991
Rosalin August Avatar asked Dec 17 '25 19:12

Rosalin August


1 Answers

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

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!