-2

Possible Duplicate:
R: convert data.frame columns from factors to characters

Follow up on question

Let's say that I upload a csv file into R

DF <- read.csv("C:/Users/David Rogers/Desktop/FILENAME.csv")

In order to perform other functions I need this file in the format as.character. Normnally, I would just do this from the very beginning:

DF <- read.csv("C:/Users/David Rogers/Desktop/FILENAME.csv", 
               header=TRUE, stringsAsFactors=FALSE)

That would give me what I need, but supposed that I don't want to upload this file from my desktop. Let's say that I already have it in R as the result of previous actions. If I had to use my method I would first have to export the file as csv, and then import it again and add the "header=TRUE, stringsAsFactors=FALSE" function like in the example above.

Is it possible to avoid this and simply convert the file, rather than exporting it and importing it back again?

Community
  • 1
  • 1
David Rogers
  • 141
  • 3
  • 11
  • 1
    It's not clear what you want to do. What's the type of data structure that you have in R? A character vector? A data frame? Paste the results of `str(x)` into your question. And please make your questions reproducible - that will mean we can help you much quicker and easier. – Andrie Nov 20 '12 at 14:21
  • I have a csv file. I can import it into R in 2 ways, listed in my question. The first will import it as a csv, the second adds header=TRUE, stringsAsFactors=FALSE). I am guessing that you know already the difference between these 2 methods. What I want to do is import the file via the 1st way, the simple way, and than convert it to get the same result as if I was uploading it in the 2nd way. I don't know how to determine what type of data I have. I upload a csv file into R, I guess I will get a dafa frame but I don't know how to check this. Also No idea what str(x) is. – David Rogers Nov 20 '12 at 14:29
  • 1
    `read.csv()` will give you a data frame. By default all text will be factors and you won't have headings. To convert this ex post, you need to remove the first line of the data frame, add headers, and then convert all `factor` columns to `character`. Or re-read the data from file, using method 2. – Andrie Nov 20 '12 at 14:32
  • Theoretically that does answer my dilemma, but practically I will have to post 3 more questions to solve it (to get the actual complete function/formula). 1 to ask how to remove the first line? 2 to ask how to add headers? 3 to ask how to convert all the factor columns to character columns. – David Rogers Nov 20 '12 at 14:38
  • 1
    Practically you now know what to search for... – Andrie Nov 20 '12 at 14:45
  • Thanks a lot. I will look it up. It is related to my previous question. The reason why your strsplit & gsub functions did not work for me was because my file was a data.frame, whilst the file in your example was a "character" file. The problem is that the initial file, I dont read it from my PC, I already have it in R (it is part of a script, the result of a lot of other actions). So in order for the example you wrote to work for me, I need to convert the file to character not re-read it because I don't have where to read it from -like I said, it is the result of a large script that I run. D – David Rogers Nov 20 '12 at 14:54

2 Answers2

2

use levels(dataframe$variable)[dataframe$variable] to do the convertion from factor to character.

Thierry
  • 18,049
  • 5
  • 48
  • 66
0

Make the first line the column names:

colnames(DF) <- DF[1,]
DF <- DF[-1,]

Convert factors to character (just a hint):

sapply(DF,class)=="factor" will get you a logical vector to select which columns are currently factors.

Ari B. Friedman
  • 71,271
  • 35
  • 175
  • 235