rafa.pereira
rafa.pereira

Reputation: 13827

Convert data frame into vector

I have a one row/column dataframe that I would like to convert into a value.

df <- data.table(x=c(300))
>#     x
># 1: 300

I've managed to do it by doing this:

b <- as.list(df)[[1]]
># [1] 300

so that identical(b, 300) == T

Is there a simpler way to achieve this? I understand this is a very simple question but I couldn't find a solution. Help?

Upvotes: 0

Views: 6479

Answers (2)

h3rm4n
h3rm4n

Reputation: 4187

Instead of using unlist, you can also simply do:

x <- df$x

the result is a normal vector with the values from x:

> x
[1] 300

Upvotes: 1

Eric Lecoutre
Eric Lecoutre

Reputation: 1481

Basically, data frames are lists, so that you can invoke unlist(df).

Upvotes: 4

Related Questions