R often understands data frame columns in a "wrong" format or you just have to change the column class from factor to character in order to modify it. I have been changing the column class in following way previously:
set.seed(1)
df <- data.frame(x = 1:10,
y = rep(1:2, 5),
k = rnorm(10, 5,2),
z = rep(c(2010, 2012, 2011, 2010, 1999), 2),
j = c(rep(c("a", "b", "c"), 3), "d"))
x <- c("y", "z")
for(i in 1:length(x)){
df[,x[i]] <- factor(df[,x[i]])}
And back to numeric:
x <- 1:5
for(i in 1:length(x)){
df[,x[i]] <- as.numeric(as.character(df[,x[i]]))} # Character cannot become numeric
It occurred to me that maybe there is a better way doing this. I found this question, which is almost exactly what I need:
convert.magic <- function(obj,types){
out <- lapply(1:length(obj),FUN = function(i){FUN1 <-
switch(types[i],
character = as.character,
numeric = as.numeric,
factor = as.factor); FUN1(obj[,i])})
names(out) <- colnames(obj)
as.data.frame(out)
}
However, for this function vector type has to be specified for each column:
convert.magic(df, rep("factor",5))
convert.magic(df, c("character", "factor"))
# Error in FUN(1:5[[1L]], ...) : could not find function "FUN1"
Could somebody help me and rebuild this function so that it works with column names and numbers, please? I am afraid that this would be too advanced for me...
x <- c("y", "z")
convert.magic(df, "character", x)