在 R 中,如何将列名打印成向量形式?
In R, how to print column names into vector form?
我有一个包含数百列的数据框。我需要对列进行分组以应用转换。必须复制粘贴,然后插入逗号并删除多余的 space 非常耗时。这是一个只有 13 列的数据框作为示例。
df <- data.frame(x1=rnorm(20),x2=rnorm(20),x3=rnorm(20)
,x4=rnorm(20),x5=rnorm(20),x6=rnorm(20),x7=rnorm(20)
,x8=rnorm(20),x9=rnorm(20),x10=rnorm(20),x11=rnorm(20)
,x12=rnorm(20),x13=c(2,1,1,2,2,1,2,1,2,2,1,1,2,1,2,2,1,2,1,1))
我可以打印名字,但它们看起来像:
colnames(df)
[1] "x1" "x2" "x3" "x4" "x5" "x6" "x7" "x8" "x9" "x10" "x11" "x12" "x13"
我要
c("x1","x2","x3","x4","x5","x6","x7","x8","x9","x10","x11","x12","x13")
我尝试过的:
paste0("c(",paste(names(df),collapse=","),")")
[1] "c(x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13)"
paste0("c(",paste(names(df),collapse="\",\""),")")
[1] "c(x1\",\"x2\",\"x3\",\"x4\",\"x5\",\"x6\",\"x7\",\"x8\",\"x9\",\"x10\",\"x11\",\"x12\",\"x13)"
paste0("c(",paste(names(df),collapse="','"),")")
[1] "c(x1','x2','x3','x4','x5','x6','x7','x8','x9','x10','x11','x12','x13)"
paste0("c(",paste(str(names(df)),collapse=","),")")
chr [1:13] "x1" "x2" "x3" "x4" "x5" "x6" "x7" "x8" "x9" "x10" "x11" "x12" "x13"
[1] "c()"
你可以试试这个:
dput(colnames(df))
输出:
c("x1", "x2", "x3", "x4", "x5", "x6", "x7", "x8", "x9", "x10",
"x11", "x12", "x13")
使用dput()
函数
df <- data.frame(x1=rnorm(20),x2=rnorm(20),x3=rnorm(20)
,x4=rnorm(20),x5=rnorm(20),x6=rnorm(20),x7=rnorm(20)
,x8=rnorm(20),x9=rnorm(20),x10=rnorm(20),x11=rnorm(20)
,x12=rnorm(20),x13=c(2,1,1,2,2,1,2,1,2,2,1,1,2,1,2,2,1,2,1,1))
x <- colnames(df)
dput(x)
#> c("x1", "x2", "x3", "x4", "x5", "x6", "x7", "x8", "x9", "x10",
#> "x11", "x12", "x13")
由 reprex package (v0.3.0)
创建于 2020-08-11
我建议安装 addin
:datapasta
,请参阅 here。
library(clipr)
x <- names(df)
#save to clipboard
write_clip(x)
#datapasta
#then paste vertically: vector_paste_vertical
c("x1",
"x2",
"x3",
"x4",
"x5",
"x6",
"x7",
"x8",
"x9",
"x10",
"x11",
"x12",
"x13")
#or paste normally: vector_paste
c("x1", "x2", "x3", "x4", "x5", "x6", "x7", "x8", "x9", "x10", "x11", "x12", "x13")
使用 addin
的优点是您可以为 vector_paste
和 vector_paste_vertical
创建快捷方式。
我有一个包含数百列的数据框。我需要对列进行分组以应用转换。必须复制粘贴,然后插入逗号并删除多余的 space 非常耗时。这是一个只有 13 列的数据框作为示例。
df <- data.frame(x1=rnorm(20),x2=rnorm(20),x3=rnorm(20)
,x4=rnorm(20),x5=rnorm(20),x6=rnorm(20),x7=rnorm(20)
,x8=rnorm(20),x9=rnorm(20),x10=rnorm(20),x11=rnorm(20)
,x12=rnorm(20),x13=c(2,1,1,2,2,1,2,1,2,2,1,1,2,1,2,2,1,2,1,1))
我可以打印名字,但它们看起来像:
colnames(df)
[1] "x1" "x2" "x3" "x4" "x5" "x6" "x7" "x8" "x9" "x10" "x11" "x12" "x13"
我要
c("x1","x2","x3","x4","x5","x6","x7","x8","x9","x10","x11","x12","x13")
我尝试过的:
paste0("c(",paste(names(df),collapse=","),")")
[1] "c(x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13)"
paste0("c(",paste(names(df),collapse="\",\""),")")
[1] "c(x1\",\"x2\",\"x3\",\"x4\",\"x5\",\"x6\",\"x7\",\"x8\",\"x9\",\"x10\",\"x11\",\"x12\",\"x13)"
paste0("c(",paste(names(df),collapse="','"),")")
[1] "c(x1','x2','x3','x4','x5','x6','x7','x8','x9','x10','x11','x12','x13)"
paste0("c(",paste(str(names(df)),collapse=","),")")
chr [1:13] "x1" "x2" "x3" "x4" "x5" "x6" "x7" "x8" "x9" "x10" "x11" "x12" "x13"
[1] "c()"
你可以试试这个:
dput(colnames(df))
输出:
c("x1", "x2", "x3", "x4", "x5", "x6", "x7", "x8", "x9", "x10",
"x11", "x12", "x13")
使用dput()
函数
df <- data.frame(x1=rnorm(20),x2=rnorm(20),x3=rnorm(20)
,x4=rnorm(20),x5=rnorm(20),x6=rnorm(20),x7=rnorm(20)
,x8=rnorm(20),x9=rnorm(20),x10=rnorm(20),x11=rnorm(20)
,x12=rnorm(20),x13=c(2,1,1,2,2,1,2,1,2,2,1,1,2,1,2,2,1,2,1,1))
x <- colnames(df)
dput(x)
#> c("x1", "x2", "x3", "x4", "x5", "x6", "x7", "x8", "x9", "x10",
#> "x11", "x12", "x13")
由 reprex package (v0.3.0)
创建于 2020-08-11我建议安装 addin
:datapasta
,请参阅 here。
library(clipr)
x <- names(df)
#save to clipboard
write_clip(x)
#datapasta
#then paste vertically: vector_paste_vertical
c("x1",
"x2",
"x3",
"x4",
"x5",
"x6",
"x7",
"x8",
"x9",
"x10",
"x11",
"x12",
"x13")
#or paste normally: vector_paste
c("x1", "x2", "x3", "x4", "x5", "x6", "x7", "x8", "x9", "x10", "x11", "x12", "x13")
使用 addin
的优点是您可以为 vector_paste
和 vector_paste_vertical
创建快捷方式。