I found a function that provides frequencies with condition and I thought of creating a function
do.call(data.frame, aggregate(X1 ~ X2, data=dat, FUN=table))
I also managed to get the column names by their index number from this thread using name <- names(dataset)[index]
.
I want to get the frequency of Xn ~ Xstatic
, where Xn
are the n-1
variables and Xstatic
is the variable of interest.
So far I made a for loop and here is my code:
library(prodlim)
NUM <- 100
dat1 <- SimSurv(NUM)
dat1$time <- sample(24:160,NUM,rep=TRUE)
dat1$X3 <- sample(0:1,NUM,rep=TRUE)
dat1$X4 <- sample(0:9,NUM,rep=TRUE)
dat1$X5 <- sample(c("a","b","c"),NUM,rep=TRUE)
dat1$X6 <- sample(c("was","que","koa","sim","sol"),NUM,rep=TRUE)
dat1$X7 <- sample(1:99,NUM,rep=TRUE)
dat1$X8 <- sample(1:200,NUM,rep=TRUE)
attach(dat1)
# EXAMPLE
# do.call(data.frame, aggregate(status ~ X6, data=dat1, FUN=table))
for( i in 1:ncol(dat1) ) {
name <- names(dat1)[i]
do.call(data.frame, aggregate(name ~ X6, data=dat1, FUN=table))
}
I get the error below and I am at a loss on how to solve this. All help is appreciated.
Error in model.frame.default(formula = name ~ X6, data = dat1) :
variable lengths differ (found for 'X6')