Specifying column names from a list in the data.frame command

I have a list called colswith column names in it:

cols <- c('Column1','Column2','Column3')

I would like to reproduce this command, but with a call to the list:

data.frame(Column1=rnorm(10))

Here is what happens when I try:

> data.frame(cols[1]=rnorm(10))

Error: unexpected '=' in "data.frame(I(cols[1])="

The same thing happens if I complete cols[1]at I()or eval().

How can I pass this element from a vector to a command data.frame()?

Update:

For some background, I defined a function calc.means()that takes a data frame and a list of variables and performs the large and complex ddply operation, adding up at the level indicated by these variables.

, data.frame(), - , calc.means() rbind() . "", rbind .

cast - ddply, , . :

cols <- c('Col1','Col2','Col3')
rbind ( calc.means(dat,cols),
    data.frame(cols[1]='All', calc.means(dat, cols[2:3])),
    data.frame(cols[1]='All', cols[2]='All', calc.means(dat, cols[3]))
)
+3
4

structure:

cols <- c("a","b")

foo <- structure(list(c(1, 2 ), c(3, 3)), .Names = cols, row.names = c(NA, -2L), class = "data.frame")

, !

+2

, , data.frame(). , data.frame() foo, :

names (foo) < - cols

+1

. :

cols_dummy <- setNames(rep(list("All"), 3), cols)

, , ,

data.frame(cols_dummy[1], calc.means(dat, cols[2:3]))

setNames(list("All"), cols[1]), , .

:

some_names <- list(name_A="Dummy 1", name_B="Dummy 2") # equivalent of cols_dummy from above
data.frame(var1=rnorm(3), some_names[1])
#        var1  name_A
# 1 -1.940169 Dummy 1
# 2 -0.787107 Dummy 1
# 3 -0.235160 Dummy 1
+1

, assign() - :

cols <- c('Col1','Col2','Col3')
data.frame(assign(cols[1], rnorm(10)))

:

   assign.cols.1...rnorm.10..
1                 -0.02056822
2                 -0.03675639
3                  1.06249599
4                  0.41763399
5                  0.38873118
6                  1.01779018
7                  1.01379963
8                  1.86119518
9                  0.35760039
10                 1.14742560

lapply() sapply() cbind(). - :

operation <- sapply(cols, function(x) data.frame(assign(x, rnorm(10))))
final     <- data.frame(lapply(operation, cbind))

:

   Col1.assign.x..rnorm.10.. Col2.assign.x..rnorm.10.. Col3.assign.x..rnorm.10..
1                0.001962187                -0.3561499               -0.22783816
2               -0.706804781                -0.4452781               -1.09950505
3               -0.604417525                -0.8425018               -0.73287079
4               -1.287038060                 0.2545236               -1.18795684
5                0.232084366                -1.0831463                0.40799046
6               -0.148594144                 0.4963714               -1.34938144
7                0.442054119                 0.2856748                0.05933736
8                0.984615916                -0.0795147               -1.91165189
9                1.222310749                -0.1743313                0.18256877
10              -0.231885977                -0.2273724               -0.43247570

, :

colnames(final) <- cols

:

          Col1       Col2        Col3
1   0.19473248  0.2864232  0.93115072
2  -1.08473526 -1.5653469  0.09967827
3  -1.90968422 -0.9678024 -1.02167873
4  -1.11962371  0.4549290  0.76692067
5  -2.13776949  3.0360777 -1.48515698
6   0.64240694  1.3441656  0.47676056
7  -0.53590163  1.2696336 -1.19845723
8   0.09158526 -1.0966833  0.91856639
9  -0.05018762  1.0472368  0.15475583
10  0.27152070 -0.2148181 -1.00551111

,

0

Source: https://habr.com/ru/post/1743217/


All Articles