R:数据框中变量名称的循环

时间:2018-02-12 09:55:29

标签: r

如何在data.frame中创建一个循环,使X1 = 2到X12 = 13?

 #Automate variable names trial
    Y <- dataL_TT$Bound_Count
    X1 <- dataL_TT$Days_conflict
    X2 <- dataL_TT$Broker360
    X3 <- dataL_TT$Broker720
    X4 <- dataL_TT$RURALPOP_2
    X5 <- dataL_TT$RURALPOP_M
    X6 <-  dataL_TT$PIP_Flag
    X7 <- dataL_TT$Trail
    X8 <- dataL_TT$Trail2
    X9 <- dataL_TT$Individual_2
    X10 <- dataL_TT$Individual_M
    X11 <- dataL_TT$Resolved_Conflict
    X12 <- dataL_TT$Priority

    d <- data.frame(Y = 1, X1 = 2, X2 = 3, X3 = 4, ....., X12 = 13)

1 个答案:

答案 0 :(得分:1)

不确定是否有很好的理解,您可以为df创建新闻栏:

d <- data.frame(Y = 1)
size <- 12 #Your Size

for(i in 1:size) d[,paste0("X",i)] <- i+1

希望这会有所帮助

Gottavianoni