I have a list like this:
[[1]]
[1] "Vigia" "PA" "1508209"
[4] "Viseu" "PA" "1508308"
[[2]]
[1] "Abaetetuba" "PA" "1500107"
[4] "Acara" "PA" "1500206"
and I want to make two matrices like this:
[,1] [,2] [,3]
[1,] Vigia PA 1508209
[2,] Viseu PA 1508308
[,1] [,2] [,3]
[1,] Abaetetuba PA 1500107
[2,] Acara PA 1508308
I've tried to use
for(i in 1:2){
output[i] <- matrix(unlist(A[[i]]), ncol = 3, byrow = TRUE)
}
But it's not working. I also tried to transform that list to a dataframe, but I couldn't.
corrected my answer so it uses a loop:
li <- list(c( "Vigia", "PA", "1508209", "Viseu", "PA", "1508308"),
c("Abaetetuba", "PA", "1500107", "Acara", "PA", "1500206"))
output <- list()
for(i in seq_along(li)){
output[[i]] <- matrix(li[[i]], nrow=2, byrow = TRUE)
}
output[[1]]
[,1] [,2] [,3] [1,] "Vigia" "PA" "1508209" [2,] "Viseu" "PA" "1508308"
output[[2]]
[,1] [,2] [,3] [1,] "Abaetetuba" "PA" "1500107" [2,] "Acara" "PA" "1500206"
But we have to be aware that matrices can only have one data type, which means in this case that everything is going to be a string!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.