如何从两列中找到最大值和最小值

时间:2021-05-03 06:40:15

标签: r

我得到了一个包含 79 个变量的数据框。 如果 79 个变量,一个变量包含国家,另一个变量包含国家的大小, 我怎样才能找到我们那两列中最大和最小的国家。 我是新手,一头雾水。

例如数据框就像:

Name      Size(m^2)  Criteria   Type
A         1200
B         1300
C         1400
D         1600
E         1900
F         2000

我想要的输出是: 最大面积:F 最小面积:A

2 个答案:

答案 0 :(得分:0)

正如@Ronak 所说,您必须发布您的数据示例或直接dput 部分数据。但是你可以简单地做:

df <- data.frame(country=letters[1:10],size=runif(10))
df$country[df$size==max(df$size)]

答案 1 :(得分:0)

数据集中的列数或行数在很大程度上与问题无关。

使用 dplyr 你可以试试这个:

library(tmap) #for country and size data
library(sf) # used to get rid of geometry in the sample set
library(dplyr) # this is the package needed to answer the question

# creata a minimal sample dataset of 50 countries

data("World") # dataset including country names and areas

set.seed(1234)

df_countries <- 
  World %>%
  st_drop_geometry() %>% 
  select(name, area) %>% 
  slice_sample(n = 50)

# to answer the question
  df_countries %>% 
  filter(area == max(area) | area == min(area)) %>% 
    arrange(desc(area))

#>         name           area
#> 1     Canada 9093510 [km^2]
#> 2 Luxembourg    2590 [km^2]

reprex package (v2.0.0) 于 2021 年 5 月 3 日创建

相关问题