如何使用r中几个变量的重复和未重复测量来创建小标题?

时间:2018-12-19 20:05:13

标签: r tidyverse tibble

我想创建一个tibble,其中包含变量的重复和未重复测量以及测量日期;

  • 3个变量(var1var2var3)在4月,5月,6月的月份中以不规则的间隔进行了16次测量
  • 4个变量(var4var5var6var7)在7月进行了一次测量
  • 2个变量(var8var9)也在7月进行了一次测量

要创建tibble,我可以使用变量日期和度量的每种组合编写向量,但是我想知道是否有一种方法可以更有效地执行此操作,因为3个变量重复进行了16次。我已经写出了这段代码,其中包含变量,日期和测量值,但是我一直呆在那里。有什么建议吗?

library (tidyverse)
variables <- c(var1, var2, var3, var4, var5, var6, var7, var8, var9)
mydates <- c(2013-04-15,
             2013-04-16,
             2013-04-17,
             2013-04-22,
             2013-04-25,
             2013-04-29,
             2013-05-02,
             2013-05-06,
             2013-05-09,
             2013-05-13,
             2013-05-16,
             2013-05-20,
             2013-05-23,
             2013-05-27,
             2013-05-30,
             2013-06-03,
             2013-07-04,  
             2013-07-08)
measurements <- c(3.2, 4.6, 1.1, 3.0, 3.6, 1.6, 1.4, 1.4, 4.8, 3.5, 4.0, 
2.7, 1.4, 2.9, 2.4, 3.6, 3.7, 4.3, 3.6, 3.5, 4.7, 1.8, 3.5, 2.4, 2.1, 1.2,
2.3, 3.9, 1.6, 2.8, 5.0, 2.4, 2.2, 2.9, 1.8, 1.7, 4.4, 3.9, 4.4, 2.6, 1.7, 
4.2, 3.4, 4.4, 4.7, 5.0, 3.0, 3.7, 2.1, 2.9, 4.5, 1.5, 2.2, 2.9)

tibble (variables, mydates, measurements)

我想要一个这样的小标题,我的前三个变量分别重复16次,我的16个初次日期重复3次,并进行测量:

variables   mydates     measurements
var1        2013-04-15  3.2
var2        2013-04-15  4.6
var3        2013-04-15  1.1
var1        2013-04-16  3.0
var2        2013-04-16  3.6
var3        2013-04-16  1.6
var1        2013-04-17  1.4
var2        2013-04-17  1.4
var3        2013-04-17  4.8
...         ...         ...  # measurements for var1, var2, var3 were repeatedly taken during the 16 first dates in the vector mydates.
var4        2013-07-04  2.1
var5        2013-07-04  2.9
var6        2013-07-04  4.5
var7        2013-07-04  1.5
var8        2013-07-08  2.2
var9        2013-07-08  2.9

1 个答案:

答案 0 :(得分:1)

这是使用base::expand.gridlubridate的(“脏”位)替代项。我将您的mydates向量转换为date类对象。

一旦您获得了variablesmydates之间的所有组合,就可以绑定measurements并使用tibble转换成as_data_frame

library (tidyverse)
library(lubridate) 


variables <- c("var1", "var2", "var3", "var4", "var5", "var6", "var7", "var8", "var9")

mydates <- c("2013-04-15",
             "2013-04-16",
             "2013-04-17",
             '2013-04-22',
             '2013-04-25',
             '2013-04-29',
             "2013-05-02",
             "2013-05-06",
             "2013-05-09",
             "2013-05-13",
             "2013-05-16",
             "2013-05-20",
             "2013-05-23",
             "2013-05-27",
             "2013-05-30",
             "2013-06-03",
             "2013-07-04",  
             "2013-07-08") %>% 
  as_date()

measurements <- c(3.2, 4.6, 1.1, 3.0, 3.6, 1.6, 1.4, 1.4, 4.8, 3.5, 4.0, 
                  2.7, 1.4, 2.9, 2.4, 3.6, 3.7, 4.3, 3.6, 3.5, 4.7, 1.8, 3.5, 2.4, 2.1, 1.2,
                  2.3, 3.9, 1.6, 2.8, 5.0, 2.4, 2.2, 2.9, 1.8, 1.7, 4.4, 3.9, 4.4, 2.6, 1.7, 
                  4.2, 3.4, 4.4, 4.7, 5.0, 3.0, 3.7, 2.1, 2.9, 4.5, 1.5, 2.2, 2.9)



mydata <- expand.grid(vars = variables[1:3], 
                      dates = mydates[month(mydates) < 7]) %>% 
  rbind(expand.grid(vars = variables[4:7], 
                    dates = mydates[month(mydates) == 7 & day(mydates) == 4])) %>% 
  rbind(expand.grid(vars = variables[8:9], 
                    dates = mydates[month(mydates) == 7 & day(mydates) == 8])) %>% 
  mutate(measures = measurements) %>% 
  as_data_frame()

输出将是:

mydata

## A tibble: 54 x 3
#   vars  dates      measures
#   <fct> <date>        <dbl>
# 1 var1  2013-04-15      3.2
# 2 var2  2013-04-15      4.6
# 3 var3  2013-04-15      1.1
# 4 var1  2013-04-16      3  
# 5 var2  2013-04-16      3.6
# 6 var3  2013-04-16      1.6
# 7 var1  2013-04-17      1.4
# 8 var2  2013-04-17      1.4
# 9 var3  2013-04-17      4.8
#10 var1  2013-04-22      3.5
## ... with 44 more rows