You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于指定列表文本批量重命名R语言数据框列名的需求

Replace Column Names in List of Data Frames with Corresponding Name Lists

To swap out the column names of each data frame in myTables with the matching name vector from myNames, you can use mapply() to iterate over both lists in parallel. Here's a clean, functional solution:

# Rename columns for every data frame in the list
myTables_renamed <- mapply(function(df, name_vec) {
  colnames(df) <- name_vec
  df
}, myTables, myNames, SIMPLIFY = FALSE)

Quick Breakdown:

  • mapply() pairs each data frame in myTables with its corresponding name list from myNames
  • The anonymous function updates the column names of the data frame and returns the modified version
  • SIMPLIFY = FALSE ensures the output stays as a list of data frames (instead of being coerced into a matrix)

Example: First Data Frame After Renaming

Here’s what the first data frame (myTables_renamed[[1]]) looks like with the column names from myNames[[1]]:

20192020202120222023
Ventas Cliente987161271477902197053629558054433708
Revenue\n\200987,1611,477,9021,970,5362,955,8054,433,708
Expenditure\n\200890,6231,337,0311,732,8732,569,1223,829,434
Profit (Loss)\n\20096,538140,871237,663386,683604,274

内容的提问来源于stack exchange,提问作者user8959427

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 14:12:35