将函数应用于数据框列表

时间:2017-07-12 14:00:34

标签: r list function dataframe lapply

我编写了一个将数据帧拆分为7个不同数据帧的函数。到目前为止一切都好。但我试图在数据帧列表中应用此功能

这是我做的清单:

m  =list(X2016_kvish_1_10t, X2015_kvish_1_10t, X2014_kvish_1_10t,
         X2013_kvish_1_10t, X2012_kvish_1_10t, X2011_kvish_1_10t,
         X2010_kvish_1_10t, X2009_kvish_1_10t, X2008_kvish_1_10t,
         X2007_kvish_1_10t, X2006_kvish_1_10t, X2005_kvish_1_10t)

这是第一个数据帧(单个数据帧)的函数:

 y = c(X2016_kvish_1_10t$nefah) # add the average traffic volume values per each day in the week (1-7)
n = 24
my.split <- function(y, n) split(y, sort(rep(letters[1:n], each=n, len=length(y)))) #function to split the Traffic volume values to 7 groups 
splitted_values = my.split(y, n)
mean_values.1_10t_2016 = lapply(splitted_values, mean) # Find the average traffic volume values for each day
mean_values.1_10t_2016 = rep(mean_values.1_10t_2016, each = 24) # Multiply the number of times the traffic volume values are displayed to fit the table

到目前为止它工作正常。但我需要为所有12个数据帧应用此功能 所以不要将y定义为:

y = c(X2016_kvish_1_10t$nefah) 

我想将y定义为:

y = c(m$nefah) # same column in each dataframe 

但是当我这样做时,它不起作用。请帮帮我

数据框看起来像这样:

> X2016_kvish_1_10t
kvish keta maslul yom nefah status                date
1       1   10      1   1  1710     NA 2016-09-11 00:00:00
2       1   10      1   1   934     NA 2016-09-11 01:00:00
3       1   10      1   1   800     NA 2016-09-11 02:00:00
4       1   10      1   1   637     NA 2016-09-11 03:00:00
5       1   10      1   1   588     NA 2016-09-11 04:00:00
6       1   10      1   1   951     NA 2016-09-11 05:00:00
7       1   10      1   1  2312     NA 2016-09-11 06:00:00
8       1   10      1   1  3769     NA 2016-09-11 07:00:00
9       1   10      1   1  3348     NA 2016-09-11 08:00:00
#168 rows in total

m的结构:

> str(m)
List of 12
$ :'data.frame':    168 obs. of  7 variables:
$ kvish : int [1:168] 1 1 1 1 1 1 1 1 1 1 ...
$ keta  : int [1:168] 10 10 10 10 10 10 10 10 10 10 ...
..$ maslul: int [1:168] 1 1 1 1 1 1 1 1 1 1 ...
..$ yom   : int [1:168] 1 1 1 1 1 1 1 1 1 1 ...
..$ nefah : int [1:168] 1710 934 800 637 588 951 2312 3769 3348 2788 ...
..$ status: int [1:168] NA NA NA NA NA NA NA NA NA NA ...
..$ date  : POSIXct[1:168], format: "2016-09-11 00:00:00" "2016-09-11 01:00:00" "2016-09-11 02:00:00" "2016-09-11 03:00:00" ...
$ :'data.frame':    168 obs. of  7 variables:
$ kvish : int [1:168] 1 1 1 1 1 1 1 1 1 1 ...
 ..$ keta  : int [1:168] 10 10 10 10 10 10 10 10 10 10 ...
 ..$ maslul: int [1:168] 1 1 1 1 1 1 1 1 1 1 ...
 ..$ yom   : int [1:168] 2 2 2 2 2 2 2 2 2 2 ...
 ..$ nefah : int [1:168] 1346 788 626 652 597 798 2344 3617 3669 2873 ...
 ..$ status: int [1:168] NA NA NA NA NA NA NA NA NA NA ...
 ..$ date  : POSIXct[1:168], format: "2015-06-15 00:00:00" "2015-06-15 01:00:00" "2015-06-15 02:00:00" "2015-06-15 03:00:00" ...
 $ :'data.frame':   168 obs. of  7 variables:
 ..$ kvish : int [1:168] 1 1 1 1 1 1 1 1 1 1 ...
 ..$ keta  : int [1:168] 10 10 10 10 10 10 10 10 10 10 ...
 ..$ maslul: int [1:168] 1 1 1 1 1 1 1 1 1 1 ...
 ..$ yom   : int [1:168] 1 1 1 1 1 1 1 1 1 1 ...
 ..$ nefah : int [1:168] 1936 966 737 596 479 765 2082 3624 3847 2960 ...
 ..$ status: int [1:168] NA NA NA NA NA NA NA NA NA NA ...
 ..$ date  : POSIXct[1:168], format: "2014-09-07 00:00:00" "2014-09-07 01:00:00" "2014-09-07 02:00:00" "2014-09-07 03:00:00" ...
  $ :'data.frame':  168 obs. of  7 variables:
..$ kvish : int [1:168] 1 1 1 1 1 1 1 1 1 1 ...
..$ keta  : int [1:168] 10 10 10 10 10 10 10 10 10 10 ...
..$ maslul: int [1:168] 1 1 1 1 1 1 1 1 1 1 ...
..$ yom   : int [1:168] 1 1 1 1 1 1 1 1 1 1 ...
..$ nefah : int [1:168] 2066 1242 875 667 676 976 2630 4337 4282 3437 ...
..$ status: int [1:168] NA NA NA NA NA NA NA NA NA NA ...
..$ date  : POSIXct[1:168], format: "2013-05-19 00:00:00" "2013-05-19 01:00:00" "2013-05-19 02:00:00" "2013-05-19 03:00:00" ...
 $ :'data.frame':   0 obs. of  7 variables:
..$ kvish : int(0) 
..$ keta  : int(0) 
..$ maslul: int(0) 
..$ yom   : int(0) 
..$ nefah : int(0) 
..$ status: int(0) 
..$ date  :Classes 'POSIXct', 'POSIXt'  atomic (0) 
..- attr(*, "tzone")= chr "UTC"
$ :'data.frame':    168 obs. of  7 variables:
..$ kvish : int [1:168] 1 1 1 1 1 1 1 1 1 1 ...
..$ keta  : int [1:168] 10 10 10 10 10 10 10 10 10 10 ...
..$ maslul: int [1:168] 1 1 1 1 1 1 1 1 1 1 ...
..$ yom   : int [1:168] 1 1 1 1 1 1 1 1 1 1 ...
..$ nefah : int [1:168] 1454 1039 700 623 459 681 1971 3369 3513 2641 ...
..$ status: int [1:168] NA NA NA NA NA NA NA NA NA NA ...
..$ date  : POSIXct[1:168], format: "2011-05-15 00:00:00" "2011-05-15 01:00:00" "2011-05-15 02:00:00" "2011-05-15 03:00:00" ...
$ :'data.frame':    168 obs. of  7 variables:
..$ kvish : int [1:168] 1 1 1 1 1 1 1 1 1 1 ...
..$ keta  : int [1:168] 10 10 10 10 10 10 10 10 10 10 ...
..$ maslul: int [1:168] 1 1 1 1 1 1 1 1 1 1 ...
..$ yom   : int [1:168] 1 1 1 1 1 1 1 1 1 1 ...
..$ nefah : int [1:168] 1405 760 471 527 637 1278 4781 5902 5462 5297 ...
..$ status: int [1:168] NA NA NA NA NA NA NA NA NA NA ...
..$ date  : POSIXct[1:168], format: "2010-05-09 00:00:00" "2010-05-09 01:00:00" "2010-05-09 02:00:00" "2010-05-09 03:00:00" ...
$ :'data.frame':    168 obs. of  7 variables:
..$ kvish : int [1:168] 1 1 1 1 1 1 1 1 1 1 ...
..$ keta  : int [1:168] 10 10 10 10 10 10 10 10 10 10 ...
..$ maslul: int [1:168] 1 1 1 1 1 1 1 1 1 1 ...
..$ yom   : int [1:168] 1 1 1 1 1 1 1 1 1 1 ...
..$ nefah : int [1:168] 2123 1479 1327 946 743 966 2419 3893 3650 3300 ...
..$ status: int [1:168] NA NA NA NA NA NA NA NA NA NA ...
..$ date  : POSIXct[1:168], format: "2009-10-11 00:00:00" "2009-10-11 01:00:00" "2009-10-11 02:00:00" "2009-10-11 03:00:00" ...
$ :'data.frame':    168 obs. of  7 variables:
..$ kvish : int [1:168] 1 1 1 1 1 1 1 1 1 1 ...
..$ keta  : int [1:168] 10 10 10 10 10 10 10 10 10 10 ...
..$ maslul: int [1:168] 1 1 1 1 1 1 1 1 1 1 ...
..$ yom   : int [1:168] 1 1 1 1 1 1 1 1 1 1 ...
..$ nefah : int [1:168] 2233 1439 1042 918 763 1024 2621 4348 4435 3742 ...
..$ status: int [1:168] NA NA NA NA NA NA NA NA NA NA ...
..$ date  : POSIXct[1:168], format: "2008-05-04 00:00:00" "2008-05-04 01:00:00" "2008-05-04 02:00:00" "2008-05-04 03:00:00" ...
$ :Classes ‘tbl_df’, ‘tbl’ and 'data.frame':    168 obs. of  7   variables:
..$ kvish : atomic [1:168] 1 1 1 1 1 1 1 1 1 1 ...
.. ..- attr(*, "label")= chr "kvish"
..$ keta  : atomic [1:168] 10 10 10 10 10 10 10 10 10 10 ...
.. ..- attr(*, "label")= chr "keta"
..$ maslul: atomic [1:168] 1 1 1 1 1 1 1 1 1 1 ...
.. ..- attr(*, "label")= chr "maslul"
..$ yom   : atomic [1:168] 1 1 1 1 1 1 1 1 1 1 ...
.. ..- attr(*, "label")= chr "yom"
..$ nefah : atomic [1:168] 1791 1130 785 717 571 ...
.. ..- attr(*, "label")= chr "nefah"
..$ status: atomic [1:168] NA NA NA NA NA NA NA NA NA NA ...
.. ..- attr(*, "label")= chr "status"
..$ date  : POSIXct[1:168], format: "2007-01-21 00:00:00" "2007-01-21 01:00:00" "2007-01-21 02:00:00" "2007-01-21 03:00:00" ...
$ :Classes ‘tbl_df’, ‘tbl’ and 'data.frame':    168 obs. of  6 variables:
..$ kvish : num [1:168] 1 1 1 1 1 1 1 1 1 1 ...
..$ keta  : num [1:168] 10 10 10 10 10 10 10 10 10 10 ...
..$ maslul: num [1:168] 1 1 1 1 1 1 1 1 1 1 ...
..$ yom   : num [1:168] 1 1 1 1 1 1 1 1 1 1 ...
..$ nefah : num [1:168] 2408 1539 1114 990 851 ...
..$ date  : POSIXct[1:168], format: "2006-09-03 00:00:00" "2006-09-03 01:00:00" "2006-09-03 02:00:00" "2006-09-03 03:00:00" ...
 $ :Classes ‘tbl_df’, ‘tbl’ and 'data.frame':   168 obs. of  6 variables:
..$ kvish : num [1:168] 1 1 1 1 1 1 1 1 1 1 ...
..$ keta  : num [1:168] 10 10 10 10 10 10 10 10 10 10 ...
..$ maslul: num [1:168] 1 1 1 1 1 1 1 1 1 1 ...
..$ yom   : num [1:168] 1 1 1 1 1 1 1 1 1 1 ...
..$ nefah : num [1:168] 2743 1759 1227 1077 1019 ...
..$ date  : POSIXct[1:168], format: "2005-05-01 00:00:00" "2005-05-01 01:00:00" "2005-05-01 02:00:00" "2005-05-01 03:00:00" ...
  

1 个答案:

答案 0 :(得分:1)

请检查它是否与您想要的有点接近......

n = 24
my.split <- function(y, n) split(y, sort(rep(letters[1:n], each=n, len=length(y)))) #function to split the Traffic volume values to 7 groups 
splitted_values = lapply(m, function(x) my.split(x$nefah, n))
mean_values <- lapply(splitted_values, function(x) lapply(x, mean)) # Find the average traffic volume values for each day
mean_values <- lapply(mean_values, unlist) 
mean_values = lapply(mean_values, function(x) rep(x, each = 24))