Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert table into a vector to use hist() on r

Tags:

r

vector

I'm learning some of R this days, I have some data on a file:

0.7300719696546073 0.5508739992014652 0.2371535688287479
0.9359803887798046 0.8249035576959065 0.3715476175169042
0.3403916445508408 0.8036890953987708 0.8079012937499597

Just like that... (but way bigger)

data = read.table("data.txt")

In some operations I would like to have all the data on a single vector to use hist(data) but I get: 'x' must be numeric

I tried different stuff like as.vector(data) or lapply(c(data), as.numeric) but nothing works.

Would you give me a hand?

SOLVED: unlist(data)
PS: Expected to not work since I have a table of 100(cols)*5000(rows)

like image 881
Noman_1 Avatar asked Dec 07 '12 12:12

Noman_1


1 Answers

unlist(data)

will do the trick. This is an easy way to transform a data frame to a vector.


With your data:

data <- read.table(text="
0.7300719696546073 0.5508739992014652 0.2371535688287479
0.9359803887798046 0.8249035576959065 0.3715476175169042
0.3403916445508408 0.8036890953987708 0.8079012937499597")

unlist(data)

The result is a vector (with named elements) :

      V11       V12       V13       V21       V22       V23       V31       V32       V33 
0.7300720 0.9359804 0.3403916 0.5508740 0.8249036 0.8036891 0.2371536 0.3715476 0.8079013 

If you don't want to have the names, use the argument use.names = FALSE in unlist:

unlist(data, use.names = FALSE)

[1] 0.7300720 0.9359804 0.3403916 0.5508740 0.8249036 0.8036891 0.2371536 0.3715476 0.8079013
like image 89
Sven Hohenstein Avatar answered Sep 22 '22 13:09

Sven Hohenstein