📜  循环后向数据添加新变量 - 无论代码示例

📅  最后修改于: 2022-03-11 15:00:24.181000             🧑  作者: Mango

代码示例1
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
}