I am trying to calculate the probabilities for a very large data set of each id for one month and I came up here in the forum with the "mutate function" however it does not really work the way i want. My data looks similar to that and I want to calculate the column P.:
ID Month Day E P
1 200701 20070101 .3 .333
1 200701 20070102 .5 .333
1 200701 20070105 .5 .333
1 200702 20070106 .6 1
2 200701 20070101 .4 .5
2 200701 20070103 .3 .5
For my trials I have subsetted the ID and Month and then simply used 1/length(df$Month). My idea now was to extract all IDs:
u <- subset(df, !duplicated(df$ID))
s <- subset(df, !duplicated(df$Month)) #Month is defined as date variable
and then mutate them with a formular similar to that:
mutate(df, p = 1/length(df$ID == u & df$month ==s))
This does not work unfortunatly.
Or do I have to do a loop?