Answers for "add columns to dataframe r loop"

1

add columns to dataframe r loop

for(i in 1:3) {                                   # Head of for-loop
  new <- rep(i, nrow(data))                       # Create new column
  data[ , ncol(data) + 1] <- new                  # Append new column
  colnames(data)[ncol(data)] <- paste0("new", i)  # Rename column name
}
Posted by: Guest on March-01-2021

Code answers related to "add columns to dataframe r loop"

Browse Popular Code Answers by Language