Bustergun
Bustergun

Reputation: 1037

R: Loop of variable names in the data frame

how do you create a loop in the data.frame so that X1 = 2 up to 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)

Upvotes: 0

Views: 46

Answers (1)

Guillaume Ottavianoni
Guillaume Ottavianoni

Reputation: 496

Not sure to have a good understanding, you can create news columns for your df :

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

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

Hope that will helps

Gottavianoni

Upvotes: 1

Related Questions