Using variable value as column name in data.frame or cbind

后端 未结 4 1620
一整个雨季
一整个雨季 2020-12-06 06:15

Is there a way in R to have a variable evaluated as a column name when creating a data frame (or in similar situations like using cbind)?

For example



        
4条回答
  •  有刺的猬
    2020-12-06 06:55

    just use colnames after creation. eg

    a <- "mycolA"
    b<- "mycolB"
    d <- data.frame(a=1:10, b=rnorm(1:10))
    colnames(d)<-c(a,b)
    d
    mycolA     mycolB
     1 -1.5873866
     2 -0.4195322
     3 -0.9511075
     4  0.2259858
     5 -0.6619433
     6  3.4669774
     7  0.4087541
     8 -0.3891437
     9 -1.6163175
     10  0.7642909
    

提交回复
热议问题