Data frame with multiple measure times

Hi!
I have a few questions concerning the subset function.
data frame example

Imagine having a data frame with several measurement times (example). All your probands answered the items at all times.

  1. Is it possible to put the answers of Person X1 (and also all the other probands) at survey time 1,2,3 etc together in one column?

2)How would you generate a subset for all people at time 2, which would consist of survey t2,t3 and t4 ?
---> when i tried:
data_subset<- (data, c(Time=="survey t2", Time=="survey t3", Time=="survey t4"))
my output consists of NAs, instead of the answers that are in the original data frame.

It is useful when asking a question to provide a "reproducible example". This isn't a static picture of your data, but a way for others to access it directly. One way to provide it is through the use of dput(your_data) or, if your data is too big, dput(head(your_data, 100)).

I'm not sure I fully understand your question, but here's a pop at what I think you were asking for!

library(tidyverse)

set.seed(321)
dat = crossing(person = c("x1", "x2", "x3"),
         time = c("t1", "t2", "t3")) %>% 
  mutate(emotion = sample(c("happy", "sad", "angry"), size = 9, replace = T))

# put in list
dat %>% 
  group_by(person) %>% 
  summarise(emotion = list(emotion))
#> # A tibble: 3 x 2
#>   person emotion  
#>   <chr>  <list>   
#> 1 x1     <chr [3]>
#> 2 x2     <chr [3]>
#> 3 x3     <chr [3]>

# put in string
dat %>% 
  group_by(person) %>% 
  summarise(emotion = paste(emotion, collapse = ", "))
#> # A tibble: 3 x 2
#>   person emotion          
#>   <chr>  <chr>            
#> 1 x1     sad, sad, happy  
#> 2 x2     happy, happy, sad
#> 3 x3     sad, happy, angry

# filter data
dat %>% 
  filter(time %in% c("t2", "t3"))
#> # A tibble: 6 x 3
#>   person time  emotion
#>   <chr>  <chr> <chr>  
#> 1 x1     t2    sad    
#> 2 x1     t3    happy  
#> 3 x2     t2    happy  
#> 4 x2     t3    sad    
#> 5 x3     t2    happy  
#> 6 x3     t3    angry

Created on 2021-12-26 by the reprex package (v2.0.1)

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.