Issue
I'm just a little undergrad student and I apologize for my silly question, but I really cannot go further in replicating an article I've found on the Internet. The code is available in Stata, but not in R.
Here the Stata Code, being each line a condition to me met by third variables, so the one I want to create just import the values of the second one.
gen child_care_basic = tuactdur24 if
(tutier1code==3 & tutier2code ==1 & tutier3code==1) |
[...]
What I tried in R was:
timeuse_2003$CHILD_CARE_BASIC <- NA
timeuse_2003$CHILD_CARE_BASIC[(timeuse_2003$TUTIER1CODE==3 & timeuse_2003$TUTIER2CODE==1 & timeuse_2003$TUTIER3CODE==1) |
(timeuse_2003$TUTIER1CODE==3 & timeuse_2003$TUTIER2CODE==1 & timeuse_2003$TUTIER3CODE==1) |
(timeuse_2003$TUTIER1CODE==3 & timeuse_2003$TUTIER2CODE==1 & timeuse_2003$TUTIER3CODE==8) |
(timeuse_2003$TUTIER1CODE==3 & timeuse_2003$TUTIER2CODE==1 & timeuse_2003$TUTIER3CODE==9) |
(timeuse_2003$TUTIER1CODE==3 & timeuse_2003$TUTIER2CODE==1 & timeuse_2003$TUTIER3CODE==10) |
(timeuse_2003$TUTIER1CODE==3 & timeuse_2003$TUTIER2CODE==1 & timeuse_2003$TUTIER3CODE==11) |
(timeuse_2003$TUTIER1CODE==3 & timeuse_2003$TUTIER2CODE==1 & timeuse_2003$TUTIER3CODE==12) |
(timeuse_2003$TUTIER1CODE==3 & timeuse_2003$TUTIER2CODE==1 & timeuse_2003$TUTIER3CODE==99) |
(timeuse_2003$TUTIER1CODE==3 & timeuse_2003$TUTIER2CODE==3) |
(timeuse_2003$TUTIER1CODE==4 & timeuse_2003$TUTIER2CODE==1 & timeuse_2003$TUTIER3CODE==1) |
(timeuse_2003$TUTIER1CODE==4 & timeuse_2003$TUTIER2CODE==1 & timeuse_2003$TUTIER3CODE==8) |
(timeuse_2003$TUTIER1CODE==4 & timeuse_2003$TUTIER2CODE==1 & timeuse_2003$TUTIER3CODE==9) |
(timeuse_2003$TUTIER1CODE==4 & timeuse_2003$TUTIER2CODE==1 & timeuse_2003$TUTIER3CODE==10) |
(timeuse_2003$TUTIER1CODE==4 & timeuse_2003$TUTIER2CODE==1 & timeuse_2003$TUTIER3CODE==10) |
(timeuse_2003$TUTIER1CODE==4 & timeuse_2003$TUTIER2CODE==1 & timeuse_2003$TUTIER3CODE==10) |
(timeuse_2003$TUTIER1CODE==4 & timeuse_2003$TUTIER2CODE==1 & timeuse_2003$TUTIER3CODE==10) |
(timeuse_2003$TUTIER1CODE==4 & timeuse_2003$TUTIER2CODE==3 & timeuse_2003$TUTIER3CODE==10) |
(timeuse_2003$TUTIER1CODE==17 & timeuse_2003$TUTIER2CODE==3 & timeuse_2003$TUTIER3CODE==10) |
(timeuse_2003$TUTIER1CODE==17 & timeuse_2003$TUTIER2CODE==4 & timeuse_2003$TUTIER3CODE==10) |
(timeuse_2003$TUTIER1CODE==17 & timeuse_2003$TUTIER2CODE==4 & timeuse_2003$TUTIER3CODE==1)
]<- timeuse_2003$TUACTDUR24
The problem is, I only got NAs in the entire column.
Again, I apologize for such a basic question, but I just tried a lot of different things and nothing worked, so I decided to ask you guys outta of my beginner's box.
Thank you in advance.
Fabricio
Solution
You can follow the pattern below to create a new column with a subset of values based on your business logic.
timeuse_2003 = data.frame(
TUACTDUR24 = c(10.1, 20.2, 30.3, 40.4, 50.5),
TUTIER1CODE = c(3, 4, 3, 4, 3),
TUTIER2CODE = c(1, 1, 1, 3, 3))
timeuse_2003
TUACTDUR24 TUTIER1CODE TUTIER2CODE
1 10.1 3 1
2 20.2 4 1
3 30.3 3 1
4 40.4 4 3
5 50.5 3 3
selection = timeuse_2003$TUTIER1CODE==3 & timeuse_2003$TUTIER2CODE==1
timeuse_2003$CHILD_CARE_BASIC = NA
timeuse_2003$CHILD_CARE_BASIC[selection] = timeuse_2003$TUACTDUR24[selection]
timeuse_2003
TUACTDUR24 TUTIER1CODE TUTIER2CODE CHILD_CARE_BASIC
1 10.1 3 1 10.1
2 20.2 4 1 NA
3 30.3 3 1 30.3
4 40.4 4 3 NA
5 50.5 3 3 NA
Answered By - BigFinger Answer Checked By - Timothy Miller (PHPFixing Admin)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.