r - 将 View 与 data.frame 子集结合使用会添加 row.names 列

标签 r dataframe

我创建两个子集(data.frames),如下所示:

sms_raw_train <- sms_raw[1:4169, ]
sms_raw_test <- sms_raw[4170:5559, ]

第一个 sms_raw_train 如下所示:

    type    text
1   ham Hope you are having a good week. Just checking in
2   ham K..give back my thanks.
3   ham Am also doing in cbe only. But have to pay.

第二个,sms_raw_test,看起来像这样:

    row.names   type    text
1   4170    ham I'm coming home 4 dinner.
2   4171    ham Come by our room at some point so we can iron out the plan for this weekend
3   4172    ham Its sunny in california. The weather's just cool

如您所见,它添加了一个 row.names 列。但是,如果我这样做:

> str(sms_raw_test[1:3, ])
'data.frame':   3 obs. of  2 variables:
 $ type: Factor w/ 2 levels "ham","spam": 1 1 1
 $ text: chr  "I'm coming home 4 dinner." "Come by our room at some point so we can iron out the plan for this weekend" "Its sunny in california. The weather's just cool"

该列实际上并不存在。

本专栏的目的是什么?为什么将其添加到 View(sms_raw_train) 中?

最佳答案

View 正在添加该列以进行显示。正如您所看到的,它实际上并不存在于子集中。

来自帮助(查看):

If there are row names on the data frame that are not 1:nrow, they are displayed in a separate first column called row.names.

sms_raw_data 的行名称(大概是)4170:5559

sms_raw_train 的行名称是 1:nrow,因此这种行为在那里并不明显。

关于r - 将 View 与 data.frame 子集结合使用会添加 row.names 列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23566691/

相关文章:

python - 如何从数据框中的 Dtype 对象获取值

r - 使用 colnames() 设置变量,使用 := operator, 变量更新 data.table 是静默更新的?

r - Rstudio 中的 %>% 键绑定(bind)/键盘快捷键

r - 通过变量名选择dplyr,一些列名是数字

R:读取 .csv 文件会删除前导零

python - Pandas dataframe applymap并行执行

r - 加速 R 计算的操作

基于一列重新编码数据框

r - 如何从多个data.frame中获取特定列并将其保存为R中的新data.frame?

r - 在散点图上绘制最大值和最小值(基于列值)