I have a loop in R that looks like the following:
for (i in c(1:10))
{
myvectorlength<-sample(c(1:4),1)
myvector<-sample(c(1:10),myvectorlength)
}
I want to build a dataframe with 10 rows that has myvector as the rows. Since myvector can differ in length, I would like to fill any missing values with NA. How would I go about this?
CodePudding user response:
You can use replicate
followed by augmenting length
to have a constant size, finally convert to dataframe
:
myvector <- replicate(10, sample(1:10, sample(1:4)))
myvector <- lapply(myvector, function(x) {
length(x) <- 4
x
})
res <- t(as.data.frame(myvector, row.names = 1:4))
rownames(res) <- 1:10
res
1 2 3 4
1 1 NA NA NA
2 8 3 NA NA
3 3 NA NA NA
4 6 NA NA NA
5 3 10 8 NA
6 7 NA NA NA
7 9 6 NA NA
8 2 NA NA NA
9 8 NA NA NA
10 3 4 NA NA
CodePudding user response:
If you really want to use a for loop:
set.seed(1005) # Set seed for reproducibility
max_col <- 4
n_rows <- 10
df <- as.data.frame(matrix(NA_real_, nrow = n_rows, ncol = max_col))
for (i in 1:n_rows) {
myvectorlength <- sample(max_col, 1)
myvector <- sample(10, myvectorlength)
df[i, 1:length(myvector)] <- myvector
}
# V1 V2 V3 V4
# 1 9 2 NA NA
# 2 9 2 5 1
# 3 8 7 NA NA
# 4 8 9 4 3
# 5 10 4 1 NA
# 6 3 NA NA NA
# 7 6 NA NA NA
# 8 2 3 10 4
# 9 9 6 10 NA
# 10 8 4 9 NA
CodePudding user response:
Try this for any different lengths of sample or rows of the data frame
lst <- list()
for (i in c(1:10))
{
myvectorlength <- sample(c(1:4), 1)
myvector <- sample(c(1:10), myvectorlength)
lst[[i]] <- myvector
}
#===================================
m <- max(sapply(lst , length))
ans <- matrix(NA , length(lst) , m)
for (i in 1:length(lst)){
for(j in 1:m){
ans[i,j] <- lst[[i]][j]
}
}
ans <- as.data.frame(ans)
#===================================
ans
#> V1 V2 V3 V4
#> 1 2 NA NA NA
#> 2 4 NA NA NA
#> 3 1 9 4 NA
#> 4 7 2 NA NA
#> 5 2 5 1 6
#> 6 10 NA NA NA
#> 7 7 2 NA NA
#> 8 6 2 3 NA
#> 9 9 10 1 NA
#> 10 7 1 5 NA
Created on 2022-06-08 by the reprex package (v2.0.1)