-1

I would like to know how can I convert a single column CSV file (with header) in to a character list in R. I've tried loading it as a data.frame in RStudio and then using as.character but the software gives me a string with random comma separated numbers.

My file has almost 1000 rows and I cannot enter them manually by using

sample = c("97_278", "BR5-045", "10-409", ...)

My file looks like this (first 5 rows)

Samples
97_278
BR5-045
10-409
BR8-316
97_420

This is what I get by using as.character under the Values table

sample_char     "c(235, 310, 103, ..."

This is what I'm looking for

sample_char     chr [1:1000] "97_278" "BR5-045" "10-409"

with sample_char being the new variable.

John
  • 353
  • 1
  • 5
  • 16

3 Answers3

0

I think the question is still not clear , the expected thing you said can be achieved by this basic function . Correct me if i get this wrong .

> a <- read.csv("cor.csv",header = T,sep = ",",stringsAsFactors = F)
> a$Samples
[1] "97_278"  "BR5-045" "10-409"  "BR8-316" "97_420" 
> class(a$Samples)
[1] "character"
Pankaj Kaundal
  • 1,012
  • 3
  • 13
  • 25
  • Thanks for your input! I've edited my post in order to explain better what I'm basically looking for. – John Jun 21 '16 at 07:19
0

If it is a single column, scan would be better to read it as a vector

res <- scan("cor.csv", sep="", what = "", quiet=TRUE)
str(res)
#chr [1:6] "Samples" "97_278" "BR5-045" "10-409" "BR8-316" ...

If we need a list of values, as.list(res) can be used.

akrun
  • 874,273
  • 37
  • 540
  • 662
0

I've ended up doing it by using as.matrix and the basic character list function (c) afterwards. So it goes like this

 sample2 =as.matrix(sample)
 sample3 = c(sample2)
John
  • 353
  • 1
  • 5
  • 16