gpt4 book ai didi

r - 如何使用 R 中的映射文件更改多个数据框中列的名称?

转载 作者:行者123 更新时间:2023-12-04 09:39:00 25 4
gpt4 key购买 nike

我有一个脚本,可以循环遍历多年的数据,一次一年。每年的数据由多个数据框组成,这些数据框放置在名为 all_input 的列表中。 .在循环开始时(读入数据后),我试图在其余处理之前以相同的格式获取所有年份的数据。

我遇到的问题是列名,它们不统一。
我想保留每个数据帧中包含 5 列,我希望它们被称为 total_emissions uom tribal_name st_usps_cddescription .在某些数据帧中,它们已经具有这些名称,而在其他数据帧中,它们具有各种名称,例如 pollutant.descpollutant_desc , 例如。

我目前的做法是这样的:

# Create a mapping file for the column names
header_map <- data.frame(orignal_col = c( "pollutant_desc", "pollutant.desc", "emissions.uom", "total.emissions", "tribal.name", "state" ),
new_col = c( "description", "description", "uom", "total_emissions", "tribal_name", "st_usps_cd" ), stringsAsFactors = FALSE)

# change the column names
lapply(all_input, function(x) {
names(x)[match(header_map$orignal_col, names(x))] <- header_map$new_col
x
}) -> all_input

这会创建一个标题映射文件,如下所示:
original_col         new_col
pollutant_desc description
pollutant.desc description
emissions.uom uom
total.emissions total_emissions
tribal.name tribal_name
state st_usps_cd

我得到的错误如下:
Error in names(x)[match(header_map$orignal_col, names(x))] <- header_map$new_col : 
NAs are not allowed in subscripted assignments

我明白,因为在处理具有不同列名的新年份数据时,我将不得不手动将条目添加到头文件中,但是我怎样才能让它工作呢?

虚假样本数据。 df1 和 df2 表示“2017”数据的格式,其中多列需要更改名称,但当前名称在数据框之间保持一致。 df3 表示“2011”数据,其中所有列名都应如此。 df4 表示“2014”数据,其中唯一需要更改的列是 pollutant_desc .请注意,每个数据框中都有不需要且可以忽略的额外列。提醒一下,这些数据帧不是同时读取的。循环按年份进行,因此将格式化和处理 df1 和 df2(在列表 all_input 中)。然后所有的数据都被删除了,一个新的 all_input list 是使用 future 几年的数据框创建的,这些数据框将具有不同的列名称。该代码必须适用于所有年份而不会更改。
> dput(df1)
structure(list(total.emissions = structure(1:2, .Label = c("100",
"300"), class = "factor"), emissions.uom = structure(1:2, .Label = c("LB",
"TON"), class = "factor"), international = c(TRUE, TRUE), hours = structure(2:1, .Label = c("17",
"3"), class = "factor"), tribal.name = structure(2:1, .Label = c("FLLK",
"SUWJG"), class = "factor"), state = structure(1:2, .Label = c("AK",
"MN"), class = "factor"), pollutant.desc = structure(1:2, .Label = c("Methane",
"NO2"), class = "factor"), policy = c(TRUE, FALSE)), class = "data.frame", row.names = c(NA,
-2L))
> dput(df2)
structure(list(total.emissions = structure(2:1, .Label = c("20",
"400"), class = "factor"), emissions.uom = structure(c(1L, 1L
), .Label = "TON", class = "factor"), international = c(FALSE,
TRUE), hours = structure(2:1, .Label = c("1", "8"), class = "factor"),
tribal.name = structure(2:1, .Label = c("SOSD", "WMFJU"), class = "factor"),
state = structure(2:1, .Label = c("SD", "WY"), class = "factor"),
pollutant.desc = structure(1:2, .Label = c("CO2", "SO2"), class = "factor"),
policy = c(FALSE, FALSE)), class = "data.frame", row.names = c(NA,
-2L))
> dput(df3)
structure(list(total_emissions = structure(2:1, .Label = c("200",
"30"), class = "factor"), uom = structure(c(1L, 1L), .Label = "TON", class = "factor"),
boundaries = structure(2:1, .Label = c("N", "Y"), class = "factor"),
tribal_name = structure(2:1, .Label = c("SOSD", "WMFJU"), class = "factor"),
st_usps_cd = structure(2:1, .Label = c("ID", "KS"), class = "factor"),
description = structure(c(1L, 1L), .Label = "SO2", class = "factor"),
policy = c(FALSE, TRUE), time = structure(1:2, .Label = c("17",
"7"), class = "factor")), class = "data.frame", row.names = c(NA,
-2L))
> dput(df4)
structure(list(total_emissions = structure(2:1, .Label = c("700",
"75"), class = "factor"), uom = structure(c(1L, 1L), .Label = "LB", class = "factor"),
tribal_name = structure(1:2, .Label = c("SSJY", "WNCOPS"), class = "factor"),
st_usps_cd = structure(1:2, .Label = c("MO", "NY"), class = "factor"),
pollutant_desc = structure(2:1, .Label = c("CO2", "Methane"
), class = "factor"), boundaries = structure(c(1L, 1L), .Label = "N", class = "factor"),
policy = c(FALSE, FALSE), time = structure(1:2, .Label = c("2",
"3"), class = "factor")), class = "data.frame", row.names = c(NA,
-2L))

谢谢!

最佳答案

尝试这个:

list_of_frames1 <- list(df1, df2, df3, df4)
list_of_frames2 <- lapply(list_of_frames1, function(x) {
nms <- intersect(names(x), header_map$orignal_col)
names(x)[ match(nms, names(x)) ] <- header_map$new_col[ match(nms, header_map$orignal_col) ]
x
})

关于r - 如何使用 R 中的映射文件更改多个数据框中列的名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62413493/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com