【问题标题】:How to split some rows into columns in R? [closed]如何将一些行拆分为R中的列? [关闭]
【发布时间】:2016-04-06 09:01:10
【问题描述】:

如果我在 R 中有这样的数据表:

Table0

一些字幕1
信息1a | info2a / info3a / info4a
信息1b | info2b / info3b / info4b
一些字幕 2
信息1c | info2c / info3c / info4c
信息1d | info2d / info3d / info4d
...

有没有办法删除所有字幕行,然后正常拆分信息列(通过|和/)?理想情况下,字幕可以放在单独的表格中。

因此,结果将是:

Table1,其中竖线代表列之间的中断:

信息1a |信息2a |信息3a | info4a
信息1b |信息2b |信息3b |信息4b
信息1c | info2c | info3c | info4c
信息1d |信息2d |信息3d | info4d
...

表2

一些字幕1
一些字幕 2
...

【问题讨论】:

  • 我不懂你的表。 Table0 中的列是什么(是 data.frame 单列,还是向量?)。什么是表 2?是单列的data.frame 吗?
  • dput 和/或str Table0....
  • 使用splitstrsplit 的组合。发布一些更有意义的示例数据以获得更好的建议.....
  • 这个问题可以通过一些样本数据轻松解决。乔:请考虑这样做。
  • 不清楚,比如“Table0”的结构是什么。是单列data.frame?两列 data.frame?

标签: r split


【解决方案1】:

切片

rowsiwant <- c(1,4)
#For the first table by removing the unwanted rows
new_table_data <- old_table[-rowsiwant,]
#Creates the second table 
new_table_labels <- old_table[rowsiwant,]

或者,如果您的行名是数字,您可以尝试使用子集函数:

#Creates the first table
new_table_labels <- subset(old_table, rownames(old_table) != rowsiwant)
#Creates the second table
new_table_data <- subset(old_table, rownames(old_table) == rowsiwant)

此外,还有很多很棒的切片资源,例如: http://statmethods.net/management/subset.html http://statistics.ats.ucla.edu/stat/r/faq/subset_R.htm

以前的问题也是一个很好的资源:

How can I subset rows in a data frame in R based on a vector of values?

Subset of table in R using row numbers?

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2016-11-20
  • 1970-01-01
  • 2023-03-10
  • 1970-01-01
  • 1970-01-01
  • 2020-03-26
  • 2018-07-22
  • 2020-10-23
相关资源
最近更新 更多