Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
246 views
in Technique[技术] by (71.8m points)

r - creating dataframe from filtering and subsetting separate dataframe?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...