I have the following data frame:
name1 name2
A B
B D
C C
D A
the columns \"name1\"
This may be a little simpler than the answer above.
#where your dataframe = df
df.name1 <- as.character (df.name1)
df.name2 <- as.character (df.name2)
I need to do things like this all the time at work because the data is so messy. I have been able to do it on import with StringsAsFactors=FALSE, but in the newest version of r I am getting an error on read.csv. Ideally I will figure that out soon... In the meantime I have been doing this as a quick and effective method. It takes the old variable, foo, which is factor type, and converts it to a new variable, fooChar, which is character type. I usually do it in situ by naming the new variable the same as the old one, but you may want to play with it before you trust it to replace values.
#Convert from Factor to Char
#Data frame named data
#Old Variable named foo, factor type
#New Variable named fooChar, character type
data$fooChar <-as.character(data$foo)
#confirm the data looks the same:
table (data$fooChar)
#confirm structure of new variable
str(data)
you're looking for as.character
, which you need to apply to each column of the data.frame
Assuming X
is your data.frame
If fctr.cols
are the names of your factor columns, then you can use:
X[, fctr.cols] <- sapply(X[, fctr.cols], as.character)
You can collect your factor columns using is.factor
:
fctr.cols <- sapply(X, is.factor)
If you want to convert only the selected column of factor variable instead of all the factor variable columns in the data frame, you can use:
file1[,n] <- sapply(file1[,n], as.character)
where n is the column number.