Hi Statistians,
Please find the reprex and crude solution as well.
Is there a elegant way to correlate between X and Y for each unique ID in fruit column ?
Extra help if you could confirm if the approach used to element NA is the best 1
Does it correlated X,Y on 2008:2011 as expected using this method ?
df <- data.frame(fruit = c(rep("Apple",4),rep("Banana",4),rep("Grapes",4)),
year = c(rep(2008:2011,3)),
X = c(NA,runif(11)),
Y = c(NA,runif(10),NA))
# This correlation accounts for all fruits and year
cor(df$X,df$Y)
# In order to get rid of NA
cor(df$X,df$Y, use = "complete.obs")
# Crude solution of obtaining correlation for each unique ID in fruit column
df.apple <- df %>% filter(fruit == "Apple")
cor(df.apple$X,df.apple$Y, use = "complete.obs")
df.banana <- df %>% filter(fruit == "Banana")
cor(df.banana$X,df.banana$Y, use = "complete.obs")
df.grape <- df %>% filter(fruit == "Grapes")
cor(df.grape$X,df.grape$Y, use = "complete.obs")
Thanks in advance,
Abi