How can I calculate a long difference in occupations from 1990 to 2021. Example:
df <- data.frame(Occ=c("petroleum engineer", "data scientist"," telemarketer" ,"welders"),
Occupation_1990=c(30, 7,50, 40),
Occupation_2021=c(40, 50, 10, 20))
for the difference of this occupations should I use diff, and how, or just - ?
If you want to find the difference between the Occupation_2021 and Occupation_1990, use the regular subtraction operator -.
df <- data.frame(Occ=c("petroleum engineer", "data scientist"," telemarketer" ,"welders"),
Occupation_1990=c(30, 7,50, 40),
Occupation_2021=c(40, 50, 10, 20))
df
Occ Occupation_1990 Occupation_2021
1 petroleum engineer 30 40
2 data scientist 7 50
3 telemarketer 50 10
4 welders 40 20
df$Difference <- df$Occupation_2021 - df$Occupation_1990
df
Occ Occupation_1990 Occupation_2021 Difference
1 petroleum engineer 30 40 10
2 data scientist 7 50 43
3 telemarketer 50 10 -40
4 welders 40 20 -20
If you want to fince differences within a vector, use diff
Vals <- c(2,6,3,12)
diff(Vals)
[1] 4 -3 9
The values 4, -3, 9 come from calculating along the vector:6 - 2 = 4
, 3 - 6 = -3
, 12 - 3 = 9
1 Like
thank you for your answers
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.