Nazanin
Nazanin

Reputation: 1

Delete last row of each subset in a list

I have a list containing several data frames. I would like to delete the last row of each data frame in the list. I used

sapply(list.name,function(d){d<- d[-nrow(d),]})

but it does not work. Does anyone know how I can do this.

Upvotes: 0

Views: 293

Answers (2)

dickoa
dickoa

Reputation: 18437

You were close, no need for assignement inside your function.

newlist <- lapply(dflist, function(d) d[-nrow(d), ])

This is a more general solution that you can adapt to other problems but in this case @textb solution more efficient.

Upvotes: 1

texb
texb

Reputation: 547

How about:

lapply(list.name,head,-1)

Upvotes: 1

Related Questions