Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data frame no longer a data frame once element is removed [duplicate]

Tags:

dataframe

r

I have a simple data frame as

myframe<-data.frame(c(NA, NA,NA, 1,2,3,4,5,NA,7,8,9))

I remove the first element like so:

myframe<-myframe[-1,]

And when I do this:

is.data.frame(myframe)

The result I get is:

[1] FALSE

I can fix it by:

myframe<-data.frame(myframe[-1,])

but I thought that the data frame wouldn't stop being a data frame after removing elements

What's happening here? I've been coding all day and my brain is fried and I cannot figure this out. Please help.

My objective is to only remove the first n occurences of NAs in a data frame. If they occur somewhere in the middle, it doesn't matter.

Thank you!

like image 660
Nepze Tyson Avatar asked May 06 '15 22:05

Nepze Tyson


1 Answers

Because you only have one column, R automatically converts the result to a vector. If you want to preserve the data frame structure you can type

myframe[-1, , drop = FALSE]
like image 56
konvas Avatar answered Oct 31 '22 00:10

konvas