gpt4 book ai didi

r - 在 R 中创建一个包含 100 列所需列名的数据框

转载 作者:行者123 更新时间:2023-12-04 20:01:05 25 4
gpt4 key购买 nike

我想创建一个包含 100 列的数据框,其名称为 feature1、feature2、feature3 ... feature100。

我能够将数据框创建为

df <- as.data.frame(matrix(0, ncol = 100, nrow = 2))

但是我的列名都是 V1、V2 等
> names(df)
[1] "V1" "V2" "V3" "V4" "V5" "V6" "V7" "V8" "V9" "V10" "V11" "V12" "V13" "V14" "V15"
[16] "V16" "V17" "V18" "V19" "V20" "V21" "V22" "V23" "V24" "V25" "V26" "V27" "V28" "V29" "V30"
[31] "V31" "V32" "V33" "V34" "V35" "V36" "V37" "V38" "V39" "V40" "V41" "V42" "V43" "V44" "V45"
[46] "V46" "V47" "V48" "V49" "V50" "V51" "V52" "V53" "V54" "V55" "V56" "V57" "V58" "V59" "V60"
[61] "V61" "V62" "V63" "V64" "V65" "V66" "V67" "V68" "V69" "V70" "V71" "V72" "V73" "V74" "V75"
[76] "V76" "V77" "V78" "V79" "V80" "V81" "V82" "V83" "V84" "V85" "V86" "V87" "V88" "V89" "V90"
[91] "V91" "V92" "V93" "V94" "V95" "V96" "V97" "V98" "V99" "V100"

然后我必须用类似的东西跟进
for(i in 1:100)
{
df[,paste("feature",i,sep="")] = df[,paste("V",i,sep="")]
df[,paste("V",i,sep="")] = NULL
}

有没有更简单的方法让我的列名称为“feature1”、“feature2”等

最佳答案

你可以做

 names(df) <- paste0('feature', 1:100)

或指定 dimnames参加 matrix称呼
as.data.frame(matrix(0, ncol=100, nrow=2, dimnames=list(NULL, 
paste0('feature',1:100))))

关于r - 在 R 中创建一个包含 100 列所需列名的数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29556113/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com