If you have a list, you can do the following:
# Reprodução da lista
for (i in 1:6) {
assign(paste('var', i, sep = '_'),
runif(30, 20, 100))
}
dataset <- cbind.data.frame(mget(ls(pattern = '*var')))
cluster <- kmeans(dataset, centers = 3)
dataset$kmeans <- as.factor(cluster[['cluster']])
mylist <- split(dataset, dataset$kmeans)
names(mylist) <- paste('datasets', seq_along(mylist), sep = '_')
mylist
is the name of the list with three data.frame
s.
Now, convert every variable, every data.frame
, in character
:
library(tidyverse)
novo <- mylist %>%
map(~mutate_all(., as.character))
novo
is the list of variables converted into each data.frame
.
prove coercion with sapply
:
sapply(novo, function(x) {
sapply(x, class)
})
datasets_1 datasets_2 datasets_3
var_1 "character" "character" "character"
var_2 "character" "character" "character"
var_3 "character" "character" "character"
var_4 "character" "character" "character"
var_5 "character" "character" "character"
var_6 "character" "character" "character"
kmeans "character" "character" "character"