14

I have the following data frame:

    name1  name2
        A      B
        B      D
        C      C
        D      A

the columns "name1" and "name2" are treated as factors and therefore A, B, C, and D are treated as levels. However I want to somehow convert this data frame so that it become

    name1  name2
      "A"    "B"
      "B"    "D"
      "C"    "C"
      "D"    "A"

In other words, convert it in a way that A, B, C, and D are treated as string.

how can i do that?

Jilber Urbina
  • 58,147
  • 10
  • 114
  • 138
user2792124
  • 421
  • 3
  • 5
  • 10
  • How are you creating the data frame? You can also do this during import if you are reading in a delimited file. – beroe Oct 06 '13 at 18:18
  • 1
    If you have the data in a csv file...you can accomplish this is during import as suggested by @beroe" `df<- read.csv("filepath", strip.white = TRUE, stringsAsFactors = FALSE)` – user3088463 Nov 09 '20 at 18:22

3 Answers3

21

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)
Ricardo Saporta
  • 54,400
  • 17
  • 144
  • 178
5

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)
Bzap
  • 51
  • 1
  • 2
1

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.