【问题标题】:Find largest pair from two columns while keeping the dataframe intact从两列中找到最大的对,同时保持数据框完整
【发布时间】:2021-01-08 02:19:39
【问题描述】:

我有一个数据框,我想在其中找到基于两列的最大对。但是,当我对数据框进行分组时,其他列的细微变化会影响我的结果。

让我告诉你:

library(plyr)

usercsv_data <- data.frame(id_str = c("89797", "12387231231", "1234823432", "3483487344", "89797", "1234823432"),
                           screen_name = c("A", "B", "C", "D", "A", "C"),
                           location = c("FL", "CO", "NYC", "MI", "FL", "NYC"),
                           verified = c("Y", "N", "N", "Y", "N", "Y"),
                           created = c("Sun", "Mon", "Tue", "Sun", "Tue", "Fri"),
                           friends_count = c(1,2,5,787,7, 5),
                           followers_count= c(2,4,6,897,4,3))

#         id_str screen_name location verified created friends_count followers_count
# 1       89797           A       FL        Y     Sun             1               2
# 2 12387231231           B       CO        N     Mon             2               4
# 3  1234823432           C      NYC        N     Tue             5               6
# 4  3483487344           D       MI        Y     Sun           787             897
# 5       89797           A       FL        N     Tue             7               4
# 6  1234823432           C      NYC        Y     Fri             5               3


#This gets me the max pairs when the groups variable are unique
plyr::ddply(usercsv_data,.(id_str,screen_name),numcolwise(max))

#         id_str screen_name friends_count followers_count
# 1  1234823432           C             5               6
# 2 12387231231           B             2               4
# 3  3483487344           D           787             897
# 4       89797           A             7               4


#BUT, when I want to do same technique with whole dataframe, I get same dataframe
plyr::ddply(usercsv_data,.(id_str,screen_name, location,verified,created),numcolwise(max))

#         id_str screen_name location verified created friends_count followers_count
# 1  1234823432           C      NYC        N     Tue             5               6
# 2  1234823432           C      NYC        Y     Fri             5               3
# 3 12387231231           B       CO        N     Mon             2               4
# 4  3483487344           D       MI        Y     Sun           787             897
# 5       89797           A       FL        N     Tue             7               4
# 6       89797           A       FL        Y     Sun             1               2

但我想要这样的东西-

#         id_str screen_name location verified created friends_count followers_count
# 1  1234823432           C      NYC        N     Tue             5               6
# 3 12387231231           B       CO        N     Mon             2               4
# 4  3483487344           D       MI        Y     Sun           787             897
# 5       89797           A       FL        N     Tue             7               4

如何进行分组以便维护所有列但只保留存在最大对的行?目前,当有更多的组变量时,它会保留唯一的变量(应该是这样)但没有足够的知识让我也搜索问题。

【问题讨论】:

  • 嗨@RonakShah,更新了我的问题。数据框中的所有其他值将与 friends_countfollowers_count 列中的最高对相关联。我只想对screen_name 进行分组,并从计数列中找到具有最大组合的行。所以其余的来自那些最大值所在的行。

标签: r dataframe plyr data-wrangling


【解决方案1】:

plyr 已停用,因此我们可以在此处使用dplyr,方法是创建一个为friends_countfollowers_count 之和的列,然后为每个id_strscreen_name 选择最大行。

library(dplyr)

usercsv_data %>%
    mutate(max = rowSums(select(., friends_count, followers_count))) %>%
    group_by(id_str, screen_name) %>%
    slice(which.max(max))

或者不创建max 列。

usercsv_data %>%
  group_by(id_str, screen_name) %>%
  slice(which.max(friends_count + followers_count))

#  id_str      screen_name location verified created friends_count followers_count
#  <chr>       <chr>       <chr>    <chr>    <chr>           <dbl>           <dbl>
#1 1234823432  C           NYC      N        Tue                 5               6
#2 12387231231 B           CO       N        Mon                 2               4
#3 3483487344  D           MI       Y        Sun               787             897
#4 89797       A           FL       N        Tue                 7               4

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-14
    • 1970-01-01
    • 1970-01-01
    • 2016-02-15
    • 2021-06-29
    相关资源
    最近更新 更多