Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete a row in a dataframe and get a dataframe back

I want to "subset" this dataframe and remove the second row using the rowname

myDataFrame <- as.data.frame(rnorm(5))
rownames(MyDataFrame) 
#"1"  "2"  "3"  "4"  "5"  

myDataFrame[-2,]
# 0.2706859  0.9708845  0.7559821 -0.2063368 

I want to be able to get the results above, but in a data frame form (with the original row names). I looked around and it seems the way to select by rowname is to use the which function, but I'm not sure how it would work in this context.

like image 570
skmathur Avatar asked Dec 04 '25 05:12

skmathur


1 Answers

You can add an argument drop = FALSE.

> mydf[-2, , drop = FALSE]
    rnorm(5)
1  1.9602780
3  0.1078827
4 -0.8517422
5 -0.8300695
like image 51
Roman Luštrik Avatar answered Dec 07 '25 05:12

Roman Luštrik



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!