Changing variable type of R dataframe inside a list -
i have list of dataframes , need transform variable in each of dataframes factor.
e.g.
mylist <- list(df1 = data.frame(a = sample(10), b = rep(1:2, 10)), df2 = data.frame(a = sample(10), b = rep(1:2, 10)) )
lets variable b needs factor in each dataframe. i've tried this:
tmp <- setnames(lapply(seq_along(mylist), function(x) apply(mylist[[x]][c("b")], 2, factor)), names(mylist))
but returns transformed variable, not whole dataframe need. know how loop, don't want resort that.
per comment david arenburg, solution should work:
tmp <- lapply(mylist, function(x) {x[, "b"] <- factor(x[, "b"]) ; x}) ; str(tmp)
Comments
Post a Comment