I create a list of dataframes with one column as character vector. But when I enclose those inside a list, all the columns get converted into character which I don't want. What is happening here?
A MWE is provided below
## Unwanted conversion to character
library(tidyverse)
df_fn_new<-function(n){
df<-data.frame(sample(n),sample(LETTERS,n),as.character(sample(50:100,n)))
}
df_new_list<-1:4 %>% map(~df_fn_new(5))
map(df_new_list,~apply(.x,2,is.numeric))
The columns are integer,factor,factor - all correct. You did not specify any numeric columns. as.data.frame converts all text to factors unless you specify stringsAsFactors = FALSE
First of all, an integer is a number as well, so it should be true. Second, the default has been changed since version 4, and R do not convert characters to factors by default any more.
@nithinmkp The reason you are getting this result is because of coercion to matrix, which converts everything to same type. This is result of using apply. Please use lapply, or even map would do the job.