How to Delete the First Row of a Dataframe in R

How to delete the first row of a dataframe in R?

Keep the labels from your original file like this:

df = read.table('data.txt', header = T)

If you have columns named x and y, you can address them like this:

df$x
df$y

If you'd like to actually delete the first row from a data.frame, you can use negative indices like this:

df = df[-1,]

If you'd like to delete a column from a data.frame, you can assign NULL to it:

df$x = NULL

Here are some simple examples of how to create and manipulate a data.frame in R:

# create a data.frame with 10 rows
> x = rnorm(10)
> y = runif(10)
> df = data.frame( x, y )

# write it to a file
> write.table( df, 'test.txt', row.names = F, quote = F )

# read a data.frame from a file:
> read.table( df, 'test.txt', header = T )

> df$x
[1] -0.95343778 -0.63098637 -1.30646529 1.38906143 0.51703237 -0.02246754
[7] 0.20583548 0.21530721 0.69087460 2.30610998
> df$y
[1] 0.66658148 0.15355851 0.60098886 0.14284576 0.20408723 0.58271061
[7] 0.05170994 0.83627336 0.76713317 0.95052671

> df$x = x
> df
y x
1 0.66658148 -0.95343778
2 0.15355851 -0.63098637
3 0.60098886 -1.30646529
4 0.14284576 1.38906143
5 0.20408723 0.51703237
6 0.58271061 -0.02246754
7 0.05170994 0.20583548
8 0.83627336 0.21530721
9 0.76713317 0.69087460
10 0.95052671 2.30610998

> df[-1,]
y x
2 0.15355851 -0.63098637
3 0.60098886 -1.30646529
4 0.14284576 1.38906143
5 0.20408723 0.51703237
6 0.58271061 -0.02246754
7 0.05170994 0.20583548
8 0.83627336 0.21530721
9 0.76713317 0.69087460
10 0.95052671 2.30610998

> df$x = NULL
> df
y
1 0.66658148
2 0.15355851
3 0.60098886
4 0.14284576
5 0.20408723
6 0.58271061
7 0.05170994
8 0.83627336
9 0.76713317
10 0.95052671

Remove an entire column from a data.frame in R

You can set it to NULL.

> 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

As pointed out in the comments, here are some other possibilities:

Data[2] <- NULL    # Wojciech Sobala
Data[[2]] <- NULL # same as above
Data <- Data[,-2] # Ian Fellows
Data <- Data[-2] # same as above

You can remove multiple columns via:

Data[1:2] <- list(NULL)  # Marek
Data[1:2] <- NULL # does not work!

Be careful with matrix-subsetting though, as you can end up with a vector:

Data <- Data[,-(2:3)]             # vector
Data <- Data[,-(2:3),drop=FALSE] # still a data.frame

Deleting first few rows and change the header names to row values

You need

names(df) <- df[3,]

then

df <- df[-c(1:3),]

df
Name1 Name2 Name3
4 Joe Paul Ross

How to remove the first row of each element in a list

We can use map

library(purrr)
library(dplyr)
ldf1 <- map(ldf, ~ .x %>%
slice(-1))

How to delete the first row from every data frame in a list of data frames?

It looks like you have a list of lists of frames (double-nested), perhaps this is reproducing that:

set.seed(42)
dfs_list <- replicate(2, replicate(3, mtcars[sample(32,3),], simplify=FALSE), simplify=FALSE)

str(dfs_list, max.level=2)
# List of 2
# $ :List of 3
# ..$ :'data.frame': 3 obs. of 11 variables:
# ..$ :'data.frame': 3 obs. of 11 variables:
# ..$ :'data.frame': 3 obs. of 11 variables:
# $ :List of 3
# ..$ :'data.frame': 3 obs. of 11 variables:
# ..$ :'data.frame': 3 obs. of 11 variables:
# ..$ :'data.frame': 3 obs. of 11 variables:

dfs_list[[1]][1:2]
# [[1]]
# mpg cyl disp hp drat wt qsec vs am gear carb
# Chrysler Imperial 14.7 8 440 230 3.23 5.345 17.42 0 0 3 4
# Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
# Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
# [[2]]
# mpg cyl disp hp drat wt qsec vs am gear carb
# Pontiac Firebird 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3 2
# Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
# Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1

From here, a double-lapply will work:

dfs_list2 <- lapply(dfs_list, function(z) lapply(z, function(y) y[-1,]))

str(dfs_list2, max.level=2)
# List of 2
# $ :List of 3
# ..$ :'data.frame': 2 obs. of 11 variables:
# ..$ :'data.frame': 2 obs. of 11 variables:
# ..$ :'data.frame': 2 obs. of 11 variables:
# $ :List of 3
# ..$ :'data.frame': 2 obs. of 11 variables:
# ..$ :'data.frame': 2 obs. of 11 variables:
# ..$ :'data.frame': 2 obs. of 11 variables:

dfs_list2[[1]][1:2]
# [[1]]
# mpg cyl disp hp drat wt qsec vs am gear carb
# Hornet Sportabout 18.7 8 360 175 3.15 3.44 17.02 0 0 3 2
# Mazda RX4 21.0 6 160 110 3.90 2.62 16.46 0 1 4 4
# [[2]]
# mpg cyl disp hp drat wt qsec vs am gear carb
# Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
# Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1

Remove one row from dataframe in R


temp1 = temp1[-1,, drop=F]
str(temp1)
'data.frame': 2 obs. of 1 variable:
$ Hamburg: chr "4562" "4604"

The default is T, which reduces the data.frame to its smallest dimension
How do I extract a single column from a data.frame as a data.frame?



Related Topics



Leave a reply



Submit