8

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 !

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

1

An option with slice

library(dplyr)
as_tibble(temp1) %>% 
    slice(-1)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.