Reputation: 71
I have the following dataframe (X1), from which I want to create a new dataframe to include only the year and the PopTotal for the years 2010:2050.
How do i extract this information into a new df? I will then be using a regression model for prediction on the df.
LocID Location year PopMale PopFemale PopTotal PopDensity
277246 900 World 1950 1266260 1270171 2536431 19.497
277247 900 World 1951 1290238 1293797 2584034 19.863
277248 900 World 1952 1313855 1317007 2630862 20.223
277249 900 World 1953 1337453 1340156 2677609 20.582
277250 900 World 1954 1361314 1363533 2724847 20.945
277251 900 World 1955 1385658 1387362 2773020 21.316
I am hoping the output will look something like this:
year PopTotal
2010 123
2011 456
... ...
2050 789
Upvotes: 0
Views: 43
Reputation: 101307
A data.table
option using between
setDT(df)[between(year, 2010, 2050), .(year, PopTotal)]
Upvotes: 0
Reputation: 887078
Here, we can subset
and select
df2 <- subset(df1, year %in% 2010:2050, select = c(year, PopTotal))
Or another option is filter
library(dplyr)
df2 <- df1 %>%
select(year, PopTotal) %>%
filter(year %in% 2010:2050)
Upvotes: 1