3

New to R, I am trying to rename/create a variable within a "for" loop, assign counter "i" to its name, and save it to a data frame. I cannot find a way to assign the counter to the name:

 df.final <- NULL
 for(i in 1:n) {
  print(i)
  v1[i] <- df$v1 ############## I need help with this
  df$v1[i] <- v1[i] ############## and this
  if(i==1){
     df.final <- df
  } else {
     df.final <- merge(df.final, df, by = "ID")
  }
 }

I found the following:

 tmpvar <- paste("v1", i, sep= "")
 assign(tmpvar, df$v1)

that helps with creating a loop-specific variable (first part), but then how do I add that variable to a data frame (second part)?

I would appreciate any help with this.

Thanks, Amir

2
  • It would help if you post what you are starting with. What is in df? Maybe you can show us two steps of your desired process? Commented Nov 25, 2018 at 7:52
  • @vaettchen: Sorry for lack of clarity. Let's assume df is a cross-section dataset that contains an ID and v1. What I had to mention so that my question would make more sense is that I am reading in a dataset in each iteration and save its v1 to a new dataset called "df.final". By running this loop, I would like to get a new dataset "df.final" that contains: ID v1_1 v1_2 ... v1_n. Hope this is more clear. Thanks, Commented Nov 25, 2018 at 14:11

3 Answers 3

2

Welcome to the R community! It would help, if you could clarify your problem a little further with example code (input, desired output). I assume, that you're trying to create a new variable in every loop? Maybe the following helps:

Let's assume we'll do 3 iterations. I'll create a simple sample dataset with 2 columns

n <- 3
# create an example dataframe with 2 columns
df<- as.data.frame(rbind(c(1,1),c(2,2),c(1,2)))
colnames(df)<- c("v1","ID")

Next, create an empty data.frame. In your last step, you cannot join by the column ID if df.final does not have a column with that name!

`df.final <- data.frame(matrix(ncol = 2, nrow = 0))
colnames(df.final)<- c("v1","ID")

Let's do the for loop

for(i in 1:n) {
  df[paste(colnames(df[1]),toString(i),sep ="_")] <- df[1] 
  if(i==n){
    df.final <- df
  } else {
    df.final <- merge(df.final, df, by = "ID") #there is no point of doing the join in my example!
  }
}

Since I am not sure, what exactly you wanted to do, I wrote some code, that copies the content of column 1 and writes it into a column that has the same name as column 1 plus a suffix of I. So let's look at the statement:

colnames(df[1]) gives you back the name of the first column of the dataframe as a string (var1) toString(i) converts your iterator i to a string By using the baste function with a sep ="_" you combine the name of the first column and your iterator to one string.

df["varname"] 

is a way to refer to a column in a dataframe.

Your output will look like this

  v1 ID v1_1 v1_2 v1_3
1  1  1    1    1    1
2  2  2    2    2    2
3  1  2    1    1    1

Hope that helps!

Sign up to request clarification or add additional context in comments.

Comments

0

I suppose this codes will achieve you. Another way of trick: when you create a intermediate dataframe, you rename the column name that equal to create column by using variable name.

for (i in 1:3){
  col_name <- paste0('add',i) 
  df_temp <- data.frame(add=runif(5))  #the new column you want to add
  colnames(df_temp) <- col_name       #change the column name 
  if (i==1){
    df_final <- df_temp
  }else{
    df_final <- cbind(df_final,df_temp)
  }
}

df_final
        add1      add2      add3
1 0.02338753 0.7503266 0.4517581
2 0.94139957 0.6460098 0.6369646
3 0.81601567 0.1825294 0.1289575
4 0.61678986 0.6715657 0.3979930
5 0.11424770 0.7712202 0.9129960

Comments

0

Thanks to both of you! Very helpful and educational for me.

Based on your answers, I am refining my question:

 n <- 5

 df.final <- NULL

 for(i in 1:n) {
 df<- as.data.frame(rbind(c(1,runif(1),100),c(2,runif(1),100),c(3,runif(1),100)))
   colnames(df)<- c("ID","v1","redundant")
   df[paste(colnames(df[2]),toString(i),sep ="_")] <- df[2] 
   df <- df[c("ID",colnames(df)[ncol(df)])] ### This works as I am referring to my new variable thorugh its column number but I still don't know how to identify the new variable, v1_i, through iterations? 
     if(i==1){
     df.final <- df
   } else {
     df.final <- merge(df.final, df, by = "ID")
   }
 }

I very recently switched to R from Stata and although I used to code in Matlab, it just takes time for me to adapt my mind to the new style of vector/matrix rather than variable/panel. Any improvement on this code is much appreciated! :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.