简体   繁体   English

从 R 中的 dataframe 中删除一行

[英]Remove one row from dataframe in R

I'm totally confused: I have a one column dataframe in R :我很困惑:我在 R 有一列 dataframe :

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 !那么剩下的就不再是 dataframe 了! 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 !我想保留 dataframe 结构只是去掉第一行!

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?默认值为 T,它将 data.frame 减少到其最小维度如何从 data.frame 中提取单个列作为 data.frame?

An option with slice slice选项

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM