Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

extracting first value from a list

Tags:

r

I would like to extract the first value from this list:

[[1]]
 [1] " \" 0.0337302" " -0.000248016" " -0.000496032" " -0.000744048"
 [5] " -0.000992063" " -0.00124008"  " -0.0014881"   " -0.00173611" 
 [9] " -0.00198413"  " -0.00223214"  " -0.00248016"  " -0.00272817" 
[13] " -0.00297619"  " -0.00322421"  " -0.00347222"  " -0.00372024" 
[17] " -0.00396825"  " -0.00421627"  " -0.00446429"  " -0.0047123"  
[21] " -0.00496032"  " -0.00520833"  " -0.00545635"  " -0.00570437" 

the name of this test is M, I have tested this M[1] and M[[1]] but I don't get the correct answer. How can I do that?

like image 389
Kaja Avatar asked Jan 06 '14 12:01

Kaja


1 Answers

You need to subset the list, and then the vector in the list:

M[[1]][1]

In other words, M is a list of 1 element, a character vector of length 24.

You may want to use unlist M to convert it to just a vector.

M <- unlist(M)

Then you can just use M[1].

To remove the \" you can use sub:

sub("\"","",M[1])
[1] "  0.0337302"
like image 184
James Avatar answered Sep 28 '22 11:09

James