tidy verse, ggplot, filter, mutate and summarise data

Hi All, Basically this is my code and I want to get the value of percentage, Category and HRLmean. when I run the code, it looks ok to me but the value of percentage, unrate or unemployed are zero. how do I find all these values? Thank you in advance!

labour_7 <- labour_2%>%
select(LABEL,IMMIG,LFSSTAT,HRLYEARN) %>%
mutate(Category=if_else(IMMIG==1,"immigrant",if_else(IMMIG==2,"immigrant",if_else(IMMIG==3,"native","")))) %>%
filter(HRLYEARN!="NA") %>%
filter(LFSSTAT!="4") %>%
group_by(LABEL) %>%
summarise(HRLmean=mean(HRLYEARN), HRLsd=sd(HRLYEARN),uemployed=sum(LFSSTAT=="3"), sum=sum(LFSSTAT),.groups = 'drop') %>%
mutate(unrate=uemployed/sum, percentage = unrate*100)

Hi!

To help us help you, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:

Hi!

The code itself seems alright to me, but as Andrés pointet out,it is hard to see what your problem is without knowing your data. S

If I could take a guess, however, I'd say that there are maybe no cases where the condition LFSSTAT==3 applies, is that possible?

If I run the relevant parts of youur code on some dummy data, it returns a percentage for label 1 where LFSSTAT==3 is true in one observation, and it returns 0 for label 2.

library(dplyr)

labour<-data.frame(LABEL=c(1,2,1,2,1),IMMIG=c(1,2,3,2,1),HRLYEARN=c(1,2,3,3,4),LFSSTAT=c(3,2,1,5,1))
labour
#>   LABEL IMMIG HRLYEARN LFSSTAT
#> 1     1     1        1       3
#> 2     2     2        2       2
#> 3     1     3        3       1
#> 4     2     2        3       5
#> 5     1     1        4       1

labour%>%
  group_by(LABEL) %>%
  summarise(HRLmean=mean(HRLYEARN), HRLsd=sd(HRLYEARN),uemployed=sum(LFSSTAT=="3"), sum=sum(LFSSTAT),.groups = 'drop') %>%
  mutate(unrate=uemployed/sum, percentage = unrate*100)

#> # A tibble: 2 x 7
#>   LABEL HRLmean HRLsd uemployed   sum unrate percentage
#>   <dbl>   <dbl> <dbl>     <int> <dbl>  <dbl>      <dbl>
#> 1     1    2.67 1.53          1     5    0.2         20
#> 2     2    2.5  0.707         0     7    0            0

Created on 2020-10-25 by the reprex package (v0.3.0)

Maybe just use labour%>%filter(LFSSTAT==3) to check if there are any cases where LFSSTAT is actually 3.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.