delete a column in r code example

Example 1: r data frame remove column

df$columnName <- NULL

Example 2: r remove inf values

df <- df[!is.infinite(rowSums(df)),]

Example 3: remove column from matrix r

# Remove third column - by column number
MyMatrix <- MyMatrix[,-3]

# Remove third and fifth columns - by feeding the matrix a boolean vector
MyMatrix <- MyMatrix[,c(TRUE,TRUE,FALSE,TRUE,FALSE,TRUE)]

Example 4: how to exclude columns in r

> Data$genome <- NULL
> head(Data)
   chr region
1 chr1    CDS
2 chr1   exon
3 chr1    CDS
4 chr1   exon
5 chr1    CDS
6 chr1   exon

Example 5: how to delete columns in df in r

> df <- data.frame(x=1:5, y=6:10, z=11:15, a=16:20)
> df <- subset (df, select = -c(x:z))
> df
a
1 16
2 17
3 18
4 19
5 20

Example 6: R drop columns

undesired <- c('mpg', 'cyl', 'hp')

mtcars <- mtcars %>%
  select(-one_of(undesired))

Tags:

C Example