Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a list into a character object in R

Tags:

r

I would like to convert a list as follows:

    list(structure(c(16L, 17L, 71L, 87L, 113L, 120L, 127L, 128L, 
      144L, 177L, 207L, 213L), .Names = c("246653_at", "246897_at", 
      "251347_at", "252988_at", "255528_at", "256535_at", "257203_at", 
      "257582_at", "258807_at", "261509_at", "265050_at", "265672_at")))

into a character object:

         c("246653_at", "246897_at", "251347_at", "252988_at", "255528_at", 
         "256535_at", "257203_at", "257582_at", "258807_at", "261509_at", 
         "265050_at", "265672_at")

I tried using

    as.character(fin[1])

which gives me

   [1] "c(16, 17, 71, 87, 113, 120, 127, 128, 144, 177, 207, 213)"

I referred to this stack overflow post but couldn't solve it.

like image 460
user1805343 Avatar asked Aug 06 '13 11:08

user1805343


People also ask

How do I convert a list to a character vector in R?

To convert List to Vector in R, use the unlist() function. The unlist() function simplifies to produce a vector by preserving all atomic components.

How do I create a list vector in R?

Converting a List to Vector in R Language – unlist() Function. unlist() function in R Language is used to convert a list to vector. It simplifies to produce a vector by preserving all components.

How do I convert something to a string in R?

To convert elements of a Vector to Strings in R, use the toString() function. The toString() is an inbuilt R function used to produce a single character string describing an R object.


2 Answers

If your object is x:

> names(unlist(x))
 [1] "246653_at" "246897_at" "251347_at" "252988_at" "255528_at" "256535_at"
 [7] "257203_at" "257582_at" "258807_at" "261509_at" "265050_at" "265672_at"
like image 185
Thomas Avatar answered Sep 24 '22 17:09

Thomas


Or just...

names(fin[[1]])
 [1] "246653_at" "246897_at" "251347_at" "252988_at" "255528_at" "256535_at"
 [7] "257203_at" "257582_at" "258807_at" "261509_at" "265050_at" "265672_at"
like image 34
Simon O'Hanlon Avatar answered Sep 21 '22 17:09

Simon O'Hanlon