7

I'm totally confused ! I have a one column dataframe in R :

temp1 = structure(list(Hamburg = c("Hamburg", "4562", "4604")), class = "data.frame", row.names = c(NA, 
-3L))

str(temp1)
'data.frame':   3 obs. of  1 variable:
 $ Hamburg: chr  "Hamburg" "4562" "4604"

When I remove the first row by :

temp1 = temp1[-1,]

then the remaining is not a dataframe any more ! and I do not have the column name as well !

temp1
[1] "4562" "4604"

str(temp1)
 chr [1:2] "4562" "4604"

How could I fix it ? I would like to keep the dataframe structure just get rid of the first row !

user9112767
  • 1,526
  • 14
  • 31

2 Answers2

7
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?

desval
  • 2,131
  • 2
  • 14
  • 21
1

An option with slice

library(dplyr)
as_tibble(temp1) %>% 
    slice(-1)
akrun
  • 789,025
  • 32
  • 460
  • 575