I have a vector that I want to turn into a single row dataframe, this code works:
vec <- c("a","b","c","d","e")
df<- as.data.frame(t(as.data.frame(vec)))
but is so clunky, does anyone have a more elegant (and preferably less computationally expensive) solution
This might be one way :
data.frame(matrix(vec, 1))
# X1 X2 X3 X4 X5
#1 a b c d e
Or changing your approach to this instead.
data.frame(t(vec))
我们也可以做
as.data.frame.list(vec)
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.