John
John

Reputation: 1947

How to DON'T change name of column while deleting rows

Let's consider :

df1<-data.frame(1:5)
colnames(df1)[1]<-'Beautiful_name'
df1
     Beautiful_name
1              1
2              2
3              3
4              4
5              5

Is there any simply solution how can I delete row from that data frame without changing name of column ?

what I mean :

> df1<-data.frame(df1[-c(1,2),])
> df1
  df1..c.1..2....
1               3
2               4
3               5

As you can see my beautiful name disappeared. Of course I can save my column name to rename new data frame after deleting but I found it very inefficient when it comes to do it for 100 data frames.

So - Is there any simple solution for that ?

Upvotes: 0

Views: 76

Answers (1)

DanY
DanY

Reputation: 6073

Add the 'drop' argument:

df1 <- df1[-c(1,2), , drop=FALSE]

Upvotes: 3

Related Questions