简体   繁体   中英

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?

基本上,数据帧是列表,因此您可以调用unlist(df)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM