Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

r - Referencing a column inside a data frame which inside another data frame

I imported the following dataframe (cm_countries) from a World Bank library:

structure(list(name = c("Afghanistan", "Albania", "Algeria"), 
    region = c("SAS", "ECS", "MEA"), incomeLevel = c("LIC", "UMC", 
    "UMC"), capitalCity = c("Kabul", "Tirane", "Algiers"), geo = structure(list(
        lat = c(34.5228, 41.3317, 36.7397), lng = c(69.1761, 
        19.8172, 3.05097)), row.names = c(NA, 3L), class = "data.frame"), 
    landlocked = c(TRUE, FALSE, FALSE)), row.names = c(NA, 3L
), class = "data.frame")
name region incomeLevel capitalCity geo.lat geo.lng landlocked
Afghanistan SAS LIC Kabul 34.52280 69.17610 TRUE
Albania ECS UMC Tirane 41.33170 19.81720 FALSE
Algeria MEA UMC Algiers 36.73970 3.05097 FALSE
question from:https://stackoverflow.com/questions/65832661/referencing-a-column-inside-a-data-frame-which-inside-another-data-frame

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

1 Answer

0 votes
by (71.8m points)

You can use base R:

#Code
new <- cm_countries[cm_countries$geo$lat>60,]

The other option is rebuild the dataframe and then filter:

#Code
cm_countries2 <- cbind(cm_countries[,-5],cm_countries$geo)
#Filter
new <- cm_countries2 %>% filter(lat>60)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...