我遇到的问题是,经过一些计算后,我的结果会出现混乱的格式。我为一年中的每个季度计算了两个不同的值,并将每个值存储在列表中,如下面的输出所示:
$`2015`
1 2 3 4
158.4567 165.7833 153.1233 140.8067
$`2016`
1 2 3 4
140.4833 149.9200 157.9233 161.2467
我之前的解决方案最终出现了类似append(value1,value2)
的内容,它为我提供了以下不需要的输出:
$`2015`
1 2 3 4 1 2 3 4
158.4567 165.7833 153.1233 140.8067 151.5633 155.2667 132.4667 128.4633
$`2016`
1 2 3 4 1 2 3 4
140.4833 149.9200 157.9233 161.2467 127.8667 144.3600 150.8467 152.5333
我找不到将这三个列表合并为一个列表或数据框的方法,如下所示:
date value1 value2
2015.1 158.4567 151.5633
2015.2 165.7833 155.2667
2015.3 153.1233 132.4667
2015.4 140.8067 128.4633
2016.1 ... ...
2016.2 ... ...
2016.3 ... ...
2016.4 ... ...
修改 示例代码,用于创建我想在数据框中合并的两个值:
library(lubridate) # for date operations
# manual created dataframe
date <- sample(seq(as.Date('1999-01-01 00:00:00'), as.Date('2017-01-01 00:00:00'), by="day"), 500)
dataM = data.frame("first" = 1:500, "sec" = c(1:500), "date"=date)
dataM <- transform(dataM, date = ymd(dataM$date)) # date to lubridate date format
splitByYear = split(dataM, year(dataM$date))
splitByQuarter = sapply(splitByYear, function(y) split(y, quarter(y$date)))
a = sapply(splitByQuarter, function(x) sapply(x, function(y) max(y$date, na.rm = TRUE)))
b = sapply(splitByQuarter, function(x) sapply(x, function(y) min(y$sec, na.rm = TRUE)))
res = mapply(quarterPP, a,b)
res
quarterPP <- function(a, b){
value1 = a+b
value2 = b+1900
c(value1, value2) # this should be in a dataframe
}
答案 0 :(得分:1)
使用matrix
中的bind_rows
和dplyr
,
#create dummy data
x <- setNames(rnorm(4), 1:4)
y <- setNames(rnorm(4), 1:4)
z <- setNames(rnorm(4), 1:4)
w <- setNames(rnorm(4), 1:4)
l1 <- list(`2015` = append(x, y), `2016` = append(z, w))
l1
#$`2015`
# 1 2 3 4 1 2 3 4
#-0.0318981 -1.1241606 -0.1040653 -0.7819973 -0.8715601 -0.2287638 -0.9092943 -0.3757804
#$`2016`
# 1 2 3 4 1 2 3 4
#-0.6034540 -1.1469930 0.6085236 1.2565788 -0.1020582 0.1383716 1.1358109 -0.2635427
l2 <- lapply(l1,function(i) {
ind <- max(as.numeric(names(i)));
data.frame(matrix(i, nrow = ind))
})
final_df <- dplyr::bind_rows(l2, .id = 'Year')
final_df$Year <- make.unique(final_df$Year)
final_df
# Year V1 V2
#1 2015 -0.0318981 -0.8715601
#2 2015.1 -1.1241606 -0.2287638
#3 2015.2 -0.1040653 -0.9092943
#4 2015.3 -0.7819973 -0.3757804
#5 2016 -0.6034540 -0.1020582
#6 2016.1 -1.1469930 0.1383716
#7 2016.2 0.6085236 1.1358109
#8 2016.3 1.2565788 -0.2635427
来自spread
的{{1}}的另一种可能性,
tidyr
答案 1 :(得分:0)
如果将所有列表合并为一个,则可以使用purrr::map_df
来迭代并将结果强制转换为data.frame:
library(tidyverse)
set.seed(47)
l1 <- set_names(replicate(3, set_names(rnorm(4), 1:4), simplify = FALSE), 2015:2017)
l2 <- set_names(replicate(3, set_names(rnorm(4), 1:4), simplify = FALSE), 2015:2017)
lst(l1, l2) %>% # make a list, pulling names from objects
map_df(~map_df(.x, # for each sublist, iterate over it
~data_frame(val = .x, # making a data.frame of each subelement
quarter = as.integer(names(.x))),
.id = 'year'), # coercing the element to a data.frame with a column of names
.id = 'var') %>% # and coerce both elements to a data.frame with a column of names
spread(var, val) # finally, reshape to wide form
#> # A tibble: 12 × 4
#> year quarter l1 l2
#> * <chr> <int> <dbl> <dbl>
#> 1 2015 1 1.99469634 0.49382018
#> 2 2015 2 0.71114251 -1.82822917
#> 3 2015 3 0.18540528 0.09147291
#> 4 2015 4 -0.28176501 0.67077922
#> 5 2016 1 0.10877555 -0.08107805
#> 6 2016 2 -1.08573747 1.26424109
#> 7 2016 3 -0.98548216 -0.70338819
#> 8 2016 4 0.01513086 -0.04057817
#> 9 2017 1 -0.25204590 -1.56616208
#> 10 2017 2 -1.46575030 0.24914817
#> 11 2017 3 -0.92245624 -0.34041599
#> 12 2017 4 0.03960243 0.41719084