Convert Rows to Columns by Matching Rows to R

I have the number of rows in a list of type <

[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 lines go dynamically ... after the "Home" there can be two, three, four, five or more subcategories .... so the binding does not work ... I have more than 5000 lines and the "Home" is usual at the beginning for each subcategory.

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 also used transpose to hide all rows in columns and when 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 will work for me either to convert rows to columns using the string match "Home"
or hidden columns to rows using a match for the string "Home" (transpose one) ....

Data

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

... ... -... node

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)
+4
3
# 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 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

:

ldply(split(x, cumsum(x == "Home")), rbind)[-1]
+4

C Braun, base:

x = c("Home", "A", "B", "C", "Home", "D", "E", "Home", "F", "G", "H", "I")
s = split(x, cumsum(x == "Home"))
max_length = max(lengths(s))

t(sapply(s, function(i) {length(i) <- max_length; return(i)}))
#   [,1]   [,2] [,3] [,4] [,5]
# 1 "Home" "A"  "B"  "C"  NA  
# 2 "Home" "D"  "E"  NA   NA  
# 3 "Home" "F"  "G"  "H"  "I" 
+4

I can't think of any built-in function for this, but you could create your version using the baseR function :

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

Source: https://habr.com/ru/post/1695364/


All Articles