Dd Pp
Dd Pp

Reputation: 5957

Build data.frame to set names?

I can convert list into data.frame with do.call function:

z=list(c(1:3),c(5:7),c(7:9))
x=as.data.frame(do.call(rbind,z))
names(x)=c("one","two","three")
x

##   one two three
## 1   1   2     3
## 2   5   6     7
## 3   7   8     9

I want to make it to be more concise ,merge the two statement into one statment,can i?

x=as.data.frame(do.call(rbind,z))  
names(x)=c("one","two","three")

Upvotes: 1

Views: 1354

Answers (2)

Gabor Csardi
Gabor Csardi

Reputation: 10825

An alternative is the structure() function, this is in base, and more general:

structure(as.data.frame(do.call(rbind,z)), names=c('a','b','c'))

Upvotes: 3

mnel
mnel

Reputation: 115382

setNames is what you want. It is in the stats package which should load with R

setNames(as.data.frame(do.call(rbind,z)), c('a','b','c'))

##   a b c
## 1 1 2 3
## 2 5 6 7
## 3 7 8 9

Upvotes: 7

Related Questions