I have a dataset that consists of a time series and monthly dummy variables (a column for each month that is 1 when the observation takes place in this month and 0 otherwise).
I wish to create a list that contains this time series for each month. For this, I created the following function:
Monthly_series <- function(var){
Monthly <- list()
months <- c('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Okt', 'Nov', 'Dec')
for (i in months){
month = noquote(i)
Monthly[[i]] <- data %>% filter(month == 1) %>% select(time, var)
}
return(Monthly)
}
However, this returns an empty list. After trying the lines outside of the function, it seems that it goes wrong at the filter() part, somehow the elements from the vector do not connect to the variables in the dataset.
Is there a way in R to filter based on values from a vector that correspond to variables in a dataframe?
CodePudding user response:
If I understand the structure of your data correctly (some code to reproduce it would be useful), here is how you can do it using sapply
and brackets for selecting columns.
df <- data.frame(var=10:1, time=1:10,
Jan=rep(1:0, each=5), Feb=rep(0:1, each=5))
# var time Jan Feb
# 1 10 1 1 0
# 2 9 2 1 0
# 3 8 3 1 0
# 4 7 4 1 0
# 5 6 5 1 0
# 6 5 6 0 1
# 7 4 7 0 1
# 8 3 8 0 1
# 9 2 9 0 1
# 10 1 10 0 1
months <- c('Jan', 'Feb')
Monthly <- sapply(months,
function(month) df[df[, month]==1, c('time', 'var')],
simplify=F)
# $Jan
# time var
# 1 1 10
# 2 2 9
# 3 3 8
# 4 4 7
# 5 5 6
#
# $Feb
# time var
# 6 6 5
# 7 7 4
# 8 8 3
# 9 9 2
# 10 10 1
CodePudding user response:
library(tidyverse)
tbl <- tibble(
var=10:1, time=1:10,
Jan=rep(1:0, each=5), Feb=rep(0:1, each=5))
res <- tbl %>%
pivot_longer(cols=-c(var, time)) %>%
filter(value > 0) %>%
select(-value) %>%
nest(data=-name) %>%
print()
# A tibble: 2 x 2
name data
<chr> <list>
1 Jan <tibble [5 x 2]>
2 Feb <tibble [5 x 2]>
print(deframe(res))
# $Jan
# A tibble: 5 x 2
var time
<int> <int>
1 10 1
2 9 2
3 8 3
4 7 4
5 6 5
$Feb
# A tibble: 5 x 2
var time
<int> <int>
1 5 6
2 4 7
3 3 8
4 2 9
5 1 10