循环浏览数据帧列表 [英] Loop through list of data frames

查看:52
本文介绍了循环浏览数据帧列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个数据帧.在每个数据框中都有一个称为Current.Net.Price的列.我想将列名更改为其他名称. 因此,我有两个列表:

I have several data frames. In each data frame there is a column called Current.Net.Price . I want to change the column names to different names. Therefore I have two lists:

Names <- c("name1","name2","name3","name4","name5")
dfList <- list(df1,df2,df3,df4,df5)

我尝试过这样的事情:

i=1
for (df in dfList) {
  names(df)[names(df) == "Current.Net.Price"] <- Names[i]
  i<-i+1
}

但是当我打电话

View(dfList$df2)

该列仍名为Current.Net.Price

the column is still named Current.Net.Price

有人可以帮我吗? :)

Could someone help me please? :)

推荐答案

检查这个简单但相似的示例. 这是有关如何访问dfList来提取有关data.frames名称的信息的全部内容.

Check this simple but similar example. It's all about how you access your dfList to exctract info about the data.frames's names.

 # data frames
dt1 = data.frame(x = 1:3,
                     y = 5:7)

dt2 = data.frame(x = 1:4,
                     z = 5:8)

dt3 = data.frame(y = 1:10,
                 x = 21:30)


Names = c("A","B","C")
dfList <- list(dt1,dt2,dt3)


for (i in 1:length(dfList)) {

  names(dfList[[i]])[names(dfList[[i]])=="x"] = Names[i]

}

这篇关于循环浏览数据帧列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆