Hey guys,
I was successful with generating a loop for almost all commands.
this is my >data.frame
:
Date OCC1990 DEGREE EMPSTAT
1 1990-01 457 1 10
2 1990-01 223 1 10
3 1990-01 221 0 10
4 1990-01 229 0 2
5 1990-02 223 1 10
6 1990-02 224 0 10
7 1990-02 225 0 10
I successfully created a loop that counts the number of people having a degree and being employed for a certain occupation. So i's in the loop depend on the occupation. The variable `COLLEMP`` refers just to people who are working (regardless the occupation) and who graduated.
for (i in 223:229) {
COLLEGE <- paste(i, sep = "_")
EMPLOYED <- paste(i, sep = "_")
cps_data[[COLLEGE]] <- ifelse(
(
(cps_data$COLLEMP %in% c(1)) &
(cps_data$OCC1990 == i)),
1*cps_data$HWTFINL, 0)
cps_data[[EMPLOYED]] <- ifelse(
(
(cps_data$EMPSTAT %in% c(10))&
(cps_data$OCC1990 == i)),
1*cps_data$HWTFINL, 0)
COLLEGESUM <- paste(i, sep = "_")
EMPLOYEDSUM <- paste(i, sep = "_")
COLLEGE_SUM <- paste(i, sep = "_")
EMPLOYED_SUM <- paste(i, sep = "_")
OCC <- paste(i, sep = "_")
COLLEGELAB[[COLLEGESUM]] <- cps_data %>%
group_by(YEAR) %>%
summarise_at(vars(COLLEGE),
list(COLLEGE_SUM = sum))
COLLEGELAB[[EMPLOYEDSUM]] <- cps_data %>%
group_by(YEAR) %>%
summarise_at(vars(EMPLOYED),
list(EMPLOYED_SUM = sum))
COLLEGELAB[[OCC]] <- cps_data %>%
group_by(YEAR) %>%
summarise_at(vars(COLLEGE, EMPLOYED),
list(COLLEGE_SUM = sum, EMPLOYED_SUM = sum))
Until there, the looping was successful and I stored my results in the data.frame COLLEGELAB
, which looks like that now:
223.YEAR 223.COLLEGE_COLLEGE_SUM 223.COLLEGE_EMPLOYED_SUM 224.YEAR 224.COLLEGE_COLLEGE_SUM 224.COLLEGE_EMPLOYED_SUM college_rate
1 1990 873221 1004000 1 1990 773221 2024000 desired value
2 1991 834501 1030000 2 1991 734501 2030000 desired value
3 1992 834543 1200000 3 1992 734543 2200000 desired value
4 1993 843500 1000050 4 1993 743500 2000050 desired value
5 1994 834510 1040000 5 1994 734510 2040000 desired value
6 1995 834340 1005000 6 1995 734340 2005000 desired value
What try to do now, is to divide for example 223.COLLEGE_COLLEGE_SUM
by 223.COLLEGE_EMPLOYED_SUM
. This seems to be quite complicated because the two variables contain the different"i's" already, which I used all the time to loop the different occupations.
college_rate <- paste(i, sep = "_")
COLLEGE_COLLEGE_SUM <- paste(i, sep = ".", "COLLEGE_COLLEGE_SUM")
COLLEGE_EMPLOYED_SUM <- paste(i, sep = ".", "COLLEGE_EMPLOYED_SUM")
#PROBLEM
COLLEGELAB <- transform(COLLEGELAB, college_rate =
COLLEGE_COLLEGE_SUM / COLLEGE_EMPLOYED_SUM)
My last idea, by using the paste
argument again, did not work due to non-numeric argument for binary operator
.
Many thanks in advance!