Answers for "Adding new variable to data after a loop"

0

Adding new variable to data after a loop

for(i in 1:5) {                             # Head of for-loop
  new_col <- rep(i, 3)                      # Creating new variable
  data[ , i] <- new_col                     # Adding new variable to data
  colnames(data)[i] <- paste0("Col_", i)    # Renaming new variable
}
Posted by: Guest on October-28-2021

Code answers related to "Adding new variable to data after a loop"

Browse Popular Code Answers by Language