2016-01-02 110 views
-2

如果我有這樣的R中的數據表:如何在R中將某些行拆分爲列?

Table0

一些字幕1
info1a | info2a/info3a/info4a
info1b | info2b/info3b/info4b
部分字幕2
info1c | info2c/info3c/info4c
info1d | info2d/info3d/info4d
...

有沒有辦法刪除所有字幕行,然後正常分裂信息列(按|和/)?字幕理想情況下可以放在單獨的表格中。

因此,結果將是:

表1,其中所述管表示列之間的遊:

info1a | info2a | info3a | info4a
info1b | info2b | info3b | info4b
info1c | info2c | info3c | info4c
info1d | info2d | info3d | info4d
...

表2

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

+0

我不明白你的表格。 Table0中的列是什麼(它是帶有單個列的data.frame,或者是一個向量?)。什麼是Table2?它是一個具有單列的'data.frame'嗎? – jbaums

+3

請'dput'和/或'str'' Table0' .... – MichaelChirico

+2

使用'split'和'strsplit'的組合。發佈一些更有意義的示例數據以獲得更好的建議..... – A5C1D2H2I1M1N2O1R2T1

回答

2

切片

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?

+0

如果我想切出更多行,該怎麼辦?像1,4,7,10 ... 1003一樣,我將如何刪除這些行?我需要輸入每個數字,還是有一個更簡單的方法? –

+0

你可以單獨添加每一行,但如果你有一個模式(比如說第三行),你可以相應地進行切片。我會推薦一個grep的字幕行(如果他們有相同的副標題)http://stackoverflow.com/questions/21311386/using-grep-to-help-subset-a-data-frame-in-r –

+0

也,http://stackoverflow.com/questions/19198492/subset-data-frame-by-complex-pattern-of-column-names&http://stackoverflow.com/questions/19417674/subset-with-pattern很好正則表達式的例子 –