简体   繁体   English

如何将一列数据框保留为数据框

[英]How to keep a column of dataframe as dataframe

Question: how to extract a column of dataframe and keep its structure as unchanged? 问题:如何提取数据框的列并保持其结构不变?

data <- iris
data[, 1] ##this will be a vector and will lose the name of the column in dataframe

data[, 1, drop = FALSE]可以解决问题。

Use list subsetting which will return a data frame: 使用列表子设置将返回一个数据帧:

data[1]

Produces 产生

  Sepal.Length
1          5.1
2          4.9
3          4.7
4          4.6
5          5.0
6          5.4
# ... omitted rows

When you use only one argument to [ with data frames it subsets data frames as lists, where each column is an element. 当您仅对数据框使用[ ,它会将数据框作为列表的子集,其中每一列都是一个元素。 It also preserves attributes, so the subset of the data frame is also a data frame. 它还保留属性,因此数据帧的子集也是数据帧。

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

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