I would like to split a dataframe into a list of dataframes and keep the classes of the variables.
# create sample data
df <- data.frame(
id=c("1","2"),
site_name = c("Zero Hedge", "Free Software Foundation"),
site_url = c("https://www.zerohedge.com", "https://www.fsf.org")
)
# specify class for site_url
class(df$site_url) <- "formula"
# split
dataframes <- split(df, df$id)
Now I wonder, why the splitted data changed the class:
class(dataframes[[1]]$site_url)
[1] "character"
My questions:
- Why does that happen?
- How can I split a dataframe into a list of dataframes and keep the classes of the variables?
Thank you for your help.
Additional info:
I came across this problem when I wanted to automatically write hyperlinks to excel files with R and openxlsxaccording to this very helpful post: Openxlsx hyperlink output display in Excel