Salty Gold Fish
Salty Gold Fish

Reputation: 463

Apply function grouping by a factor and return variable length results for each group

I have a data frame as follows

id   value group
001   5      A
002   6      B
003   -1     A
004   -100   C
005   7      A
006   9      B
007   -4     B
008   200    C

I want to get those ids with positive value for each group. The result is as follows

A
001, 005
B
002, 006
C
008

How do I implement this in R? Thanks in advance!

Upvotes: 1

Views: 69

Answers (3)

A. Suliman
A. Suliman

Reputation: 13135

Using dplyr

library(dplyr)
df %>% group_by(group) %>% summarise(ids=toString(id[value>0]))

# A tibble: 3 x 2
  group ids     
  <chr> <chr>   
1 A     001, 005
2 B     002, 006
3 C     008 

Use purrr::map/purrr::map_chr if you would like the output as a list/character vector.

purrr::map(split(df,df$group), ~toString(.$id[.$value>0]))
$`A`
[1] "001, 005"

$B
[1] "002, 006"

$C
[1] "008"

purrr::map_chr(split(df,df$group), ~toString(.$id[.$value>0]))
         A          B          C 
"001, 005" "002, 006"      "008" 

Upvotes: 4

Ronak Shah
Ronak Shah

Reputation: 389175

Using subset with split in base R

with(subset(df, value > 0), split(id, group))

#$A
#[1] "001" "005"

#$B
#[1] "002" "006"

#$C
#[1] "008"

data

df <- structure(list(id = c("001", "002", "003", "004", "005", "006", 
"007", "008"), value = c("5", "6", "-1", "-100", "7", "9", "-4", 
"200"), group = c("A", "B", "A", "C", "A", "B", "B", "C")), class = 
"data.frame", row.names = c(NA, -8L))

Upvotes: 4

Carles
Carles

Reputation: 2829

Using dplyr, one can filter out those <0 out and then split by groups as following:

library(data.table)
library(dplyr)
Data<-fread("
id   value group
001   5      A
002   6      B
003   -1     A
004   -100   C
005   7      A
006   9      B
007   -4     B
008   200    C")
Data%>%  
  filter(value>0)%>%
  select(id, group) %>% 
  group_split(group, keep = FALSE)%>%
  setNames(unique(Data$group))
# $A
# # A tibble: 2 x 1
# id
# <int>
# 1     1
# 2     5
# 
# $B
# # A tibble: 2 x 1
# id
# <int>
# 1     2
# 2     6
# 
# $C
# # A tibble: 1 x 1
# id
# <int>
# 1     8

Upvotes: 0

Related Questions