Home > Back-end >  Is it possible in R to group by variable and then create correlation inside column?
Is it possible in R to group by variable and then create correlation inside column?

Time:01-07

my problem is that I would like to do group by and then correlation inside column in data frame.

For example:

Year Var
2000 10
2010 15
2010 13
2000 11

And I want to group by year, so it would look like this:

Year Var
2000 10
2000 11
2010 15
2010 13

And than create correlation between year 2000 and 2010.

Is it understandable?

Thank you for any advice.

Richard

So far I have this:

prep_cor <- all_species_df %>% group_by(Sheet) %>% slice_sample(n=600) %>% arrange(Sheet, desc(BLUE))

And I am expecting to have result in a table.

CodePudding user response:

Turn data into wide format and then compute correlation:

df%>%
  group_by(Year)%>%
  mutate(ID=row_number())%>%
  spread(Year,Var)%>%
  select(-ID)%>%
  cor

     2000 2010
2000    1   -1
2010   -1    1
  • Related