Creating a complicated new variable
I have a dataset which is in longformat in which Measurements (Time) are
nested in Networkpartners (NP) which are nested in Persons (ID), here is
an example of what it looks like (the real dataset has over thousands of
rows):
ID NP Time Outcome1 Outcome2
1 11 1 4 NA
1 11 2 3 4
1 11 3 NA NA
1 12 1 2 3
1 12 2 3 1
1 12 3 3 2
2 21 1 2 4
2 21 2 NA NA
2 21 3 NA NA
2 22 1 4 NA
2 22 2 4 3
2 22 3 NA 4
Now I would like to create the following new variable "NP.T":
The Number of Networkpartners (who have no NA in outcome1 and outcome2 at
this measurement) a specific person (ID) at a specific time.
So I would like to create a dataset like this:
ID NP Time Outcome1 Outcome2 NP.T
1 11 1 4 NA 2
1 11 2 3 4 2
1 11 3 NA NA 1
1 12 1 2 3 2
1 12 2 3 1 2
1 12 3 3 2 1
2 21 1 2 4 2
2 21 2 NA NA 1
2 21 3 NA NA 1
2 22 1 4 NA 2
2 22 2 4 3 1
2 22 3 NA 4 1
I have the solution on how to create a variable that counts the number of
Networkpartners (who have no NA in ONE of the two outcomes) a specific
person (ID) at a specific time:
library(plyr)
mydata1<-ddply(mydata,.(ID,Time),transform,
NP.T=length(Outcome[which(Outcome ! ="NA")]))
Now, I'd be very thankful if somebody could help me to find an answer on
my specific problem!
No comments:
Post a Comment