Reputation: 1017
I have the following vector:
dput(z1)
c(Measure = "Height", IFS = "12,636.00", IFS = "15,401.00",
IFS = "15,371.00", IFS = "16,218.00", IFS = "20,975.00", Measure = "Height",
IFS = "12,258.00", IFS = "12,542.00", IFS = "13,014.00", IFS = "13,508.00",
IFS = "11,934.00", Measure = "Height", IFS = "5,854.46", IFS = "6,500.39",
IFS = "5,208.78", IFS = "4,221.37", NA, Measure = "Height",
IFS = "845.70", IFS = "1,161.30", IFS = "1,217.80", IFS = "875.90",
IFS = "401.90", Measure = "Height", IFS = "34,416.00", IFS = "33,249.00",
IFS = "32,942.00", IFS = "33,174.00", IFS = "33,267.00", Measure = "Height",
IFS = "2,390.00", IFS = "2,269.00", IFS = "1,902.00", IFS = "1,710.20",
IFS = "1,734.60")
I would like to transform it into a data frame that would look like this:
Measure IFS IFS IFS IFS IFS
Height 12,636.00 15,401.00 15,371.00 16,218.00 20,975.00
Height 12,258.00 12,542.00 13,014.00 13,508.00 11,934.00
Height 5,854.46 6,500.39 5,208.78 4,221.37 NA
... plus the three more rows. That is the first 6 values are a row and so on.
I tried
z1<-stack(z1) then
z1<-t(z1)
z1<-as.data.frame(z1)
z1<-matrix( z1, ncol=6)
But the data is not arranging the way I need it. Thanks
Upvotes: 1
Views: 53
Reputation: 887831
We could split
into a list
, based on the presence of 'Height' and collapse them by rbind
ing with map_dfr
library(purrr)
library(dplyr)
z2 <- z1[complete.cases(z1)]
map_dfr(split(z2, cumsum(z2 %in% 'Height')), as.data.frame.list)
-output
Measure IFS IFS.1 IFS.2 IFS.3 IFS.4
1 Height 12,636.00 15,401.00 15,371.00 16,218.00 20,975.00
2 Height 12,258.00 12,542.00 13,014.00 13,508.00 11,934.00
3 Height 5,854.46 6,500.39 5,208.78 4,221.37 <NA>
4 Height 845.70 1,161.30 1,217.80 875.90 401.90
5 Height 34,416.00 33,249.00 32,942.00 33,174.00 33,267.00
6 Height 2,390.00 2,269.00 1,902.00 1,710.20 1,734.60
Upvotes: 1