通过匹配R中的字符串将行转换为列

时间:2018-03-26 18:40:09

标签: r dataframe grep dplyr tidyr

我在列表中有多行,例如'

[1,]  "Home"
[2,]  "A"
[3,]  "B"
[4,]  "C"
[5,]  "Home"
[6,]  "D"
[7,]  "E"
[8,]  "Home"
[9,]  "F"
[10,] "G"
[11,] "H"
[12,] "I"

这些行动态地......在" Home"可以有两个,三个,四个,五个或更多子类....所以绑定不起作用...我有超过5000行和" Home"在每个子类别的开头都很常见..

我希望它看起来像这样。

       [,1]   [,2] [,3] [,4] [,5]

[1,]  "Home"  "A"  "B"  "C"   
[2,]  "Home"  "D"  "E"
[3,]  "Home"  "F"  "G"  "H"  "I"

OR

我还使用transpose将所有行转换为列 并且我使用了转置。

   [,1]    [,2] [,3] [,4]  [,5]   [,6]  [,7]  [,8]   [,9] [,10] [,11] [,12]

   "Home"  "A"  "B"  "C"  "Home"   "D"   "E"  "Home"  "F"  "G"   "H"   "I"

任何解决方案对我来说都可以使用" Home"
的字符串匹配将行转换为列 要么 使用" Home"将列隐藏到行中字符串匹配(转置一个)....

数据

x <- c("Home", "A", "B", "C", "Home", "D", "E", "Home", "F", "G", "H", "I")
x <- matrix(x)

问题已经解决了......谢谢你的回复...... 我通过其他方式做到了...通过在循环中运行它并在每个节点结束后添加行

List <- c() 

#loop start
nodes <- html_nodes(file,".class a b c ") %>% html_text()
List[[length(List)+1]] = nodes      
#loop ends

library(stringi)
catdf <- stri_list2matrix(List, byrow = TRUE)
catdf <- as.data.frame(catdf)

3 个答案:

答案 0 :(得分:4)

# create the data
x <- as.matrix(c("Home", "A", "B", "C", "Home", "D", "E", "Home", "F" ,"G" ,"H" ,"I"))

# split the data into a list of vectors, wherever "Home" is found
rowstarts <- x == "Home"
rowlist <- split(x, cumsum(rowstarts))

然后,我们可以使用plyr的{​​{1}}函数将列表绑定到单个数据框中:

ldply

并将所有内容放在一起,简短的单行:

> plyr::ldply(rowlist, rbind)[-1]
     1 2 3    4    5
1 Home A B    C <NA>
2 Home D E <NA> <NA>
3 Home F G    H    I

答案 1 :(得分:4)

与C Braun的答案类似,但保留在{"data " : null}

base

答案 2 :(得分:2)

我无法想到任何内置函数来执行此操作,但您可以使用base R函数创建自己的函数:

vector.split <- function(x, sep = "Home") {
    bool.sep <- x == sep
    split(x[!bool.sep], cumsum(bool.sep)[!bool.sep])
}