通过匹配R中的字符串将行转换为列 [英] Convert Rows into Columns by matching string in R
问题描述
[1,]Home
[2 ,A
[3,]B
[4,]C
[5,]主页
[6,]D$
[10,]G
[11,b $ b [7,]E
[8,]Home ]H
[12,]I
这些行是动态发布的。 ..家后可以有两个,三个,四个,五个或更多的子类别....所以绑定不起作用...我有超过5000行,首页在每个子类别的开始是常见的。 。
我希望它看起来像这样。
[,1] [,2] [,3] [,4] [,5]
[1,]HomeABC
[2,]HomeDE
[3,]HomeF GHI
OR
我还使用转置将所有行转换为列
,并使用转置我得到。
[ ,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
家ABC家DE家FGHI
任何解决方案对我来说都是合适的,可以使用Home字符串匹配将行转换为列,即
或
使用Home字符串匹配(转置一个)将列转换为行。
数据
<$ p (Home),A,B,C,Home,D,E,Home,F, ,G,H,I)
x < - 矩阵(x)
这个问题已经解决了...谢谢你的回应...
我以其他方式做了通过在循环中运行它并在每个单节点结束后添加该行
List< - c()
#loop start
nodes< - html_nodes(file,.class abc)%>%html_text()
List [[length(List)+1] ] =节点
#loop结束
库(stringi)
catdf< - stri_list2matrix(List,byrow = TRUE)
catdf< - as.data。 frame(catdf)
<$ c $ (c(Home,A,B,C,Home,D,E,Home)创建数据
x< - as.matrix ,F,G,H,I))
#将数据拆分为矢量列表,无论发现Home,
rowstarts< - x ==Home
rowlist< - split(x,cumsum(rowstarts))
<然后,我们可以使用 plyr
的 ldply
函数将列表绑定到单个数据框中:
> plyr :: ldply(rowlist,rbind)[ - 1]
1 2 3 4 5
1首页A B C< NA>
2 Home D E< NA> < NA>
3 Home FGHI
综合起来,
ldply(split(x,cumsum(x ==Home)),rbind)[ - 1]
I have number of rows in a list like '
[1,] "Home"
[2,] "A"
[3,] "B"
[4,] "C"
[5,] "Home"
[6,] "D"
[7,] "E"
[8,] "Home"
[9,] "F"
[10,] "G"
[11,] "H"
[12,] "I"
these rows are coming dynamically...after "Home" there can be two,three,four,five or more subcategories....so binding is not working... I have more than 5000 rows and "Home" is common in the start for every subcategories..
I Want it to look like this.
[,1] [,2] [,3] [,4] [,5]
[1,] "Home" "A" "B" "C"
[2,] "Home" "D" "E"
[3,] "Home" "F" "G" "H" "I"
OR
I have also used transpose to covert all rows into columns and on using transpose I got.
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
"Home" "A" "B" "C" "Home" "D" "E" "Home" "F" "G" "H" "I"
any solution would work for me either converting rows in to columns using string match of "Home"
or
covert columns into rows using "Home" string match(transpose one)....
Data
x <- c("Home", "A", "B", "C", "Home", "D", "E", "Home", "F", "G", "H", "I")
x <- matrix(x)
The question has been solved...Thankyou for your response... I did it other way around...by running it in a loop and adding the row after every single node ends
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)
# 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))
We can then use plyr
's ldply
function to bind the list into a single data frame:
> 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
And put all together it makes a short one-liner:
ldply(split(x, cumsum(x == "Home")), rbind)[-1]
这篇关于通过匹配R中的字符串将行转换为列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!