I have the following data:
rand.sum <- function(n){
x <- sort(runif(n-1))
c(x,1) - c(0,x)
}
M <- t(replicate(8,rand.sum(8))) # data frame with probabilities
Test1 <- as.data.frame(t(replicate(1, rand.sum(8))))
Test2 <- as.data.frame(t(replicate(1, rand.sum(8))))
Test <- rbind(Test1,Test2) # Test data
ID1 <- c("ID1")
ID2 <- c("ID2")
ID <- rbind(ID1,ID2) #ID's
data <- cbind(ID,Test)
Now with the help of the awesome Community I have the following apply function:
gemeinden_new <- lapply(1:dim(Test)[1], function(z)
as.data.frame(
matrix(sapply(1:8, function(i) sum(data[z,2:9] * M[,i])),
nrow=1, ncol=8)
))
Now I want to add a new column and fill it based with data from the df "data".
I have come up with the following solution:
library(tibble)
gemeinden_new <- lapply(gemeinden_new, function(x){
add_column(x, gemeindeschluessel = 0, .before = 1)
})
for(i in 1:dim(Test)[1]) {
z <- i
gemeindeschluessel <- paste(data[z,1])
gemeinden_new[[z]][z,1] <- gemeindeschluessel
}
While this works it is not very elegant and because I am working with a large dataset I would like to work around for-loops. Is there any possibility to re-write the for-loop using lapply? I was not able to do so.
The final list solution should look like this:
# $ID1
# gemeindeschluessel V1 V2 V3 V4 V5 V6 V7 V8
# 1 ID1 0.0598796 0.1526457 0.08604147 0.2314867 0.06307882 0.2047462 0.07962943 0.122492
# $ID2
# gemeindeschluessel V1 V2 V3 V4 V5 V6 V7 V8
# 1 ID2 0.1385492 0.1047066 0.06278719 0.1710685 0.09209054 0.2519348 0.06434532 0.1145178