R 一个DF的数据帧列表;从列表中的每个DF提取日期列,将所有值传递给单个DF(带列1985-2017)
我有一个169个数据帧(assetcount_dfs)的列表,对应于地理网格上的正方形,每个正方形都包含一组资产。我想填写一个单独的数据框,计算1985-2017年每一天每平方米开始的资产数量 以下是此数据帧列表的结构:R 一个DF的数据帧列表;从列表中的每个DF提取日期列,将所有值传递给单个DF(带列1985-2017),r,purrr,R,Purrr,我有一个169个数据帧(assetcount_dfs)的列表,对应于地理网格上的正方形,每个正方形都包含一组资产。我想填写一个单独的数据框,计算1985-2017年每一天每平方米开始的资产数量 以下是此数据帧列表的结构: Square1_DF (3 rows/assets) | x | y | dates char[1989, N/A, 1991] ... Square169_DF (1 row/asset) | x | y | dates char[2002] 我想将其转换为一
Square1_DF (3 rows/assets) | x | y | dates char[1989, N/A, 1991]
...
Square169_DF (1 row/asset) | x | y | dates char[2002]
我想将其转换为一个数据帧,在“dateDF”中计算这些日期:
这是我的数据的一个玩具样本。在assetcount\u dfs
中的每个数据框中,“val”列表示我要填充dateDF的日期:
sdf1我会使用tidyverse()
来处理这个问题。不要试图在循环中编辑dateDF
,而是计算一年与一个数据帧ID一起出现的频率,然后将数据重塑为您想要的格式
library(tidyverse)
assets2 <- assetcount_dfs %>%
# combine all the small data frames into a single big df
bind_rows(.id = 'rowdf') %>%
# toss out the N/A values so they don't get counted
filter(val != "#N/A")
simpleDateDF <- assets2 %>%
# count each year and what data frame it's from
count(rowdf, val) %>%
# spread the years out into columns, using 0 as the default
spread(val, n, fill = 0)
库(tidyverse)
资产2%
#将所有小数据帧组合成一个大数据帧
绑定_行(.id='rowdf')%>%
#扔掉N/A值,这样它们就不会被计算在内
过滤器(val!=“不适用”)
简化的F%
#每年统计一次,它来自于什么样的数据框架
计数(rowdf,val)%>%
#将年份分散到列中,使用0作为默认值
排列(val,n,填充=0)
什么决定了在dateDF
中计数应该进入哪一行?显然,计数会进入相应年份对应的列中,但您如何知道将特定计数放入哪一行?另外,什么是assetcount\u dfs
?在assetcount\u dfs
中,每个网格方格都有一个唯一的id:“75923”“75924”“76565”
等等。所以我给了dateDF
相同的行名,这些方格的唯一id<代码>(rownames(dateDF)以及什么决定了特定年份的事件计数应该进入哪一行?列表中的每个数据帧都有ID,即列表的名称。因此,我从数据帧的“dates”列(此处val
)提取的所有值应该使用相同的ID进入dateDF
中的行。每当我在其中一个数据帧中遇到1985到2017之间的值时,我希望增加相应的列(例如dateDF
中的X2000)和行(由初始列表ID定义)1.这就是为什么我将dateDF
rownames设置为列表中数据帧的ID不清楚“列表中的每个数据帧都有ID”是什么意思。在任何数据帧中都没有标记为ID的列,在dateDF
中也没有类似于“sdf1”的任何内容或者与较小数据帧中的a
或x
列共享的任何值。可能还有169个较小的数据帧,每个数据帧对应一行?
sdf1 <- data.frame(a = c("1","4","5","1"), x = c("sdf","asf","asdf","sdf"), val = c("2014","2012","#N/A", "2001"))
sdf2 <- data.frame(a = c("1","4"), x = c("sdf","asdf"), val = c("#N/A","2011"))
sdf3 <- data.frame(a = c("1","4","5","1","1"), x = c("sdf","asf","asdf","sdf","sdf"), val = c("2010","2015","2000","2002", "2003"))
assetcount_dfs <- list(sdf1 = sdf1,sdf2 = sdf2,sdf3 = sdf3)
date_range <- 1985:2017
dateDF <- data.frame(matrix(ncol = length(date_range),nrow = 3)) # actual length is 169 rows, only using 3 for this example
colnames(dateDF) <- paste0('X',1985:2017) # name columns 'X'DATE
rownames(dateDF) <- names(assetcount_dfs)
dateDF[] <- 0 # filled with zeroes
invisible(map(listx, function(df) {
for (i in df$val){
if (as.integer(i) %in% 1985:2017){
datesDF_colName <- paste0('X',i)
dateDF[substitute(df), datesDF_colName] <- dateDF[[datesDF_colName]] + 1
# Attempt to set dateDF value at [grid-square DF's name / row, Column based on Year ]
}
}}))
# Output:
# Error in `[<-.data.frame`(`*tmp*`, substitute(df), datesDF_colName, value =
# c(1, :
# anyNA() applied to non-(list or vector) of type 'language'
# Called from: `[<-.data.frame`(`*tmp*`, substitute(df), datesDF_colName,
# value = c(1,
# 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
# 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
# Note my sample code for 'listx' for some reason generates DFs with factors, although I am currently dealing with character arrays.
library(tidyverse)
assets2 <- assetcount_dfs %>%
# combine all the small data frames into a single big df
bind_rows(.id = 'rowdf') %>%
# toss out the N/A values so they don't get counted
filter(val != "#N/A")
simpleDateDF <- assets2 %>%
# count each year and what data frame it's from
count(rowdf, val) %>%
# spread the years out into columns, using 0 as the default
spread(val, n, fill = 0)