在聚合函数中显示多列,包括 R 中的 strings/characters
Showing multiple columns in aggregate function including strings/characters in R
R 菜鸟问题在这里。
假设我有这个数据框:
City State Pop
Fresno CA 494
San Franciso CA 805
San Jose CA 945
San Diego CA 1307
Los Angeles CA 3792
Reno NV 225
Henderson NV 257
Las Vegas NV 583
Gresham OR 105
Salem OR 154
Eugene OR 156
Portland OR 583
Fort Worth TX 741
Austin TX 790
Dallas TX 1197
San Antonio TX 1327
Houston TX 2100
我想知道每个州每排第三低的人口,即:
City State Pop
San Jose CA 945
Las Vegas NV 583
Eugene OR 156
Dallas TX 1197
我试过这个:
ord_pop_state <- aggregate(Pop ~ State , data = ord_pop, function(x) { x[3] } )
我得到了这个:
State Pop
CA 945
NV 583
OR 156
TX 1197
为了获得包括城市在内的所需输出,我在这方面缺少什么?
我建议尝试 data.table
包来完成这样的任务,因为语法更简单,代码更高效。我还建议添加 order
函数以确保数据已排序
library(data.table)
setDT(ord_pop)[order(Pop), .SD[3L], keyby = State]
# State City Pop
# 1: CA San Jose 945
# 2: NV Las Vegas 583
# 3: OR Eugene 156
# 4: TX Dallas 1197
所以基本上,首先数据按 Pop
排序,然后我们按 State
对 .SD
(数据本身的符号参数)进行子集化
虽然这也很容易用 base R 解决(我们假设数据在这里排序),我们可以只为每个组创建一个索引,然后根据该索引做一个简单的子集
ord_pop$indx <- with(ord_pop, ave(Pop, State, FUN = seq))
ord_pop[ord_pop$indx == 3L, ]
# City State Pop indx
# 3 San Jose CA 945 3
# 8 Las Vegas NV 583 3
# 11 Eugene OR 156 3
# 15 Dallas TX 1197 3
这是一个 dplyr
版本:
df2 <- df %>%
group_by(state) %>% # Group observations by state
arrange(-pop) %>% # Within those groups, sort in descending order by pop
slice(3) # Extract the third row in each arranged group
这是我用来测试它的玩具数据:
set.seed(1)
df <- data.frame(state = rep(LETTERS[1:3], each = 5), city = rep(letters[1:5], 3), pop = round(rnorm(15, 1000, 100), digits=0))
这是它的输出; 'b' 在每种情况下都是第三大的巧合,而不是代码中的故障:
> df2
Source: local data frame [3 x 3]
Groups: state
state city pop
1 A b 1018
2 B b 1049
3 C b 1039
在 R 中,使用不同的 packages.Choice 包可以实现相同的最终结果,这是效率和代码简单性之间的权衡。
由于您来自强大的 SQL 背景,这可能更易于使用:
library(sqldf)
#Example to return 3rd lowest population of a State
result <-sqldf('Select City,State,Pop from data order by Pop limit 1 offset 2;')
#Note the SQL query is a sample and needs to be modifed to get desired result.
R 菜鸟问题在这里。
假设我有这个数据框:
City State Pop
Fresno CA 494
San Franciso CA 805
San Jose CA 945
San Diego CA 1307
Los Angeles CA 3792
Reno NV 225
Henderson NV 257
Las Vegas NV 583
Gresham OR 105
Salem OR 154
Eugene OR 156
Portland OR 583
Fort Worth TX 741
Austin TX 790
Dallas TX 1197
San Antonio TX 1327
Houston TX 2100
我想知道每个州每排第三低的人口,即:
City State Pop
San Jose CA 945
Las Vegas NV 583
Eugene OR 156
Dallas TX 1197
我试过这个:
ord_pop_state <- aggregate(Pop ~ State , data = ord_pop, function(x) { x[3] } )
我得到了这个:
State Pop
CA 945
NV 583
OR 156
TX 1197
为了获得包括城市在内的所需输出,我在这方面缺少什么?
我建议尝试 data.table
包来完成这样的任务,因为语法更简单,代码更高效。我还建议添加 order
函数以确保数据已排序
library(data.table)
setDT(ord_pop)[order(Pop), .SD[3L], keyby = State]
# State City Pop
# 1: CA San Jose 945
# 2: NV Las Vegas 583
# 3: OR Eugene 156
# 4: TX Dallas 1197
所以基本上,首先数据按 Pop
排序,然后我们按 State
.SD
(数据本身的符号参数)进行子集化
虽然这也很容易用 base R 解决(我们假设数据在这里排序),我们可以只为每个组创建一个索引,然后根据该索引做一个简单的子集
ord_pop$indx <- with(ord_pop, ave(Pop, State, FUN = seq))
ord_pop[ord_pop$indx == 3L, ]
# City State Pop indx
# 3 San Jose CA 945 3
# 8 Las Vegas NV 583 3
# 11 Eugene OR 156 3
# 15 Dallas TX 1197 3
这是一个 dplyr
版本:
df2 <- df %>%
group_by(state) %>% # Group observations by state
arrange(-pop) %>% # Within those groups, sort in descending order by pop
slice(3) # Extract the third row in each arranged group
这是我用来测试它的玩具数据:
set.seed(1)
df <- data.frame(state = rep(LETTERS[1:3], each = 5), city = rep(letters[1:5], 3), pop = round(rnorm(15, 1000, 100), digits=0))
这是它的输出; 'b' 在每种情况下都是第三大的巧合,而不是代码中的故障:
> df2
Source: local data frame [3 x 3]
Groups: state
state city pop
1 A b 1018
2 B b 1049
3 C b 1039
在 R 中,使用不同的 packages.Choice 包可以实现相同的最终结果,这是效率和代码简单性之间的权衡。
由于您来自强大的 SQL 背景,这可能更易于使用:
library(sqldf)
#Example to return 3rd lowest population of a State
result <-sqldf('Select City,State,Pop from data order by Pop limit 1 offset 2;')
#Note the SQL query is a sample and needs to be modifed to get desired result.