如何使用 LOOP 简化此 R 脚本代码。它是用于分析年度数据的代码
How to use LOOP to simplify this R script code. It is a code for analysing a yearly data
我正在分析年度数据集。我想重塑和重命名数据。如何使用 LOOP 来简化以下代码?
marryc2010 <- data.frame(date=c2010$Date,EC=c2010$`Elemental Carbon by transmittance-PM10`,OC=c2010$`Organic Carbon by transmittance-PM10`)
marryc2011 <- data.frame(date=c2011$Date,EC=c2011$`Elemental Carbon by transmittance-PM10`,OC=c2011$`Organic Carbon by transmittance-PM10`)
marryc2012 <- data.frame(date=c2012$Date,EC=c2012$`Elemental Carbon by transmittance-PM10`,OC=c2012$`Organic Carbon by transmittance-PM10`)
marryc2013 <- data.frame(date=c2013$Date,EC=c2013$`Elemental Carbon by transmittance-PM10`,OC=c2013$`Organic Carbon by transmittance-PM10`)
marryc2014 <- data.frame(date=c2014$Date,EC=c2014$`Elemental Carbon by transmittance-PM10`,OC=c2014$`Organic Carbon by transmittance-PM10`)
marryc2015 <- data.frame(date=c2015$Date,EC=c2015$`Elemental Carbon by transmittance-PM10`,OC=c2015$`Organic Carbon by transmittance-PM10`)
marryc2016 <- data.frame(date=c2016$Date,EC=c2016$`Elemental Carbon by transmittance-PM10`,OC=c2016$`Organic Carbon by transmittance-PM10`)
marryc2017 <- data.frame(date=c2017$Date,EC=c2017$`Elemental Carbon by transmittance-PM10`,OC=c2017$`Organic Carbon by transmittance-PM10`)
marryc2018 <- data.frame(date=c2018$Date,EC=c2018$`Elemental Carbon by transmittance-PM10`,OC=c2018$`Organic Carbon by transmittance-PM10`)
marryc2019 <- data.frame(date=c2019$Date,EC=c2019$`Elemental Carbon by transmittance-PM10`,OC=c2019$`Organic Carbon by transmittance-PM10`)
一种简单的方法是在您的数据帧列表上使用 purrr::map
。
library(purrr)
library(dplyr)
# Create a list of the dataframes, and return values as object names.
df.list <- mget(paste0("c", 2010:2019))
# Then, use map to extract the columns that you need from each dataframe.
dfs.updated <-
purrr::map(
.x = df.list,
.f = function(x) {
x %>% dplyr::select(
Date,
`Elemental Carbon by transmittance-PM10`,
`Organic Carbon by transmittance-PM10`
)
}
)
# Then, can rename if needed.
names(dfs.updated) <- paste0("marry", 2010:2019)
您不应创建 10 个单独的数据框。您应该在列表中创建它们,以便稍后可以使用 lapply
将它们捆绑在一起。
input_list <- mget( paste0('c', 2010:2019) # Now inputs are in one list
marry_list <- lapply( input_list, function(x) {
data.frame(date=x$Date,
EC=x$`Elemental Carbon by transmittance-PM10`,
OC=x$`Organic Carbon by transmittance-PM10`)}
names(marry_list) <- paste0("marry", 2010:2019)
当您需要获得其中之一时,非常简单:
df_val <- marry_list[['marry2015']]
或
df_val <- marry_list$marry2015
如果您只想要其中一个的日期列,则可以是以下之一:
marry_list$marry2019$date # both `$` and `[[` can be chained
marry_list[['marry2019']][['date']] # use `[[` inside function, `$` at console
我正在分析年度数据集。我想重塑和重命名数据。如何使用 LOOP 来简化以下代码?
marryc2010 <- data.frame(date=c2010$Date,EC=c2010$`Elemental Carbon by transmittance-PM10`,OC=c2010$`Organic Carbon by transmittance-PM10`)
marryc2011 <- data.frame(date=c2011$Date,EC=c2011$`Elemental Carbon by transmittance-PM10`,OC=c2011$`Organic Carbon by transmittance-PM10`)
marryc2012 <- data.frame(date=c2012$Date,EC=c2012$`Elemental Carbon by transmittance-PM10`,OC=c2012$`Organic Carbon by transmittance-PM10`)
marryc2013 <- data.frame(date=c2013$Date,EC=c2013$`Elemental Carbon by transmittance-PM10`,OC=c2013$`Organic Carbon by transmittance-PM10`)
marryc2014 <- data.frame(date=c2014$Date,EC=c2014$`Elemental Carbon by transmittance-PM10`,OC=c2014$`Organic Carbon by transmittance-PM10`)
marryc2015 <- data.frame(date=c2015$Date,EC=c2015$`Elemental Carbon by transmittance-PM10`,OC=c2015$`Organic Carbon by transmittance-PM10`)
marryc2016 <- data.frame(date=c2016$Date,EC=c2016$`Elemental Carbon by transmittance-PM10`,OC=c2016$`Organic Carbon by transmittance-PM10`)
marryc2017 <- data.frame(date=c2017$Date,EC=c2017$`Elemental Carbon by transmittance-PM10`,OC=c2017$`Organic Carbon by transmittance-PM10`)
marryc2018 <- data.frame(date=c2018$Date,EC=c2018$`Elemental Carbon by transmittance-PM10`,OC=c2018$`Organic Carbon by transmittance-PM10`)
marryc2019 <- data.frame(date=c2019$Date,EC=c2019$`Elemental Carbon by transmittance-PM10`,OC=c2019$`Organic Carbon by transmittance-PM10`)
一种简单的方法是在您的数据帧列表上使用 purrr::map
。
library(purrr)
library(dplyr)
# Create a list of the dataframes, and return values as object names.
df.list <- mget(paste0("c", 2010:2019))
# Then, use map to extract the columns that you need from each dataframe.
dfs.updated <-
purrr::map(
.x = df.list,
.f = function(x) {
x %>% dplyr::select(
Date,
`Elemental Carbon by transmittance-PM10`,
`Organic Carbon by transmittance-PM10`
)
}
)
# Then, can rename if needed.
names(dfs.updated) <- paste0("marry", 2010:2019)
您不应创建 10 个单独的数据框。您应该在列表中创建它们,以便稍后可以使用 lapply
将它们捆绑在一起。
input_list <- mget( paste0('c', 2010:2019) # Now inputs are in one list
marry_list <- lapply( input_list, function(x) {
data.frame(date=x$Date,
EC=x$`Elemental Carbon by transmittance-PM10`,
OC=x$`Organic Carbon by transmittance-PM10`)}
names(marry_list) <- paste0("marry", 2010:2019)
当您需要获得其中之一时,非常简单:
df_val <- marry_list[['marry2015']]
或
df_val <- marry_list$marry2015
如果您只想要其中一个的日期列,则可以是以下之一:
marry_list$marry2019$date # both `$` and `[[` can be chained
marry_list[['marry2019']][['date']] # use `[[` inside function, `$` at console