简体   繁体   中英

Nested loop and if in R with combination replacement

I have the following dataframe:

df1 <- data.frame(
  nutriment = c("glucose", "fructose", "lipid", "iron", "vitamin"),
  family = c("A", "B" , "B", "C", "D"),
  rank = c(1, 2 , 2, 3, 4),
  indicator = c(1, 1, 0, 1, 1)
)

I would like to do the following.

Compare two families each time, whenever one has a rank greater then the other, then the nutriment with indicator==1 associated with this family becomes independent, ie his new family =="independent"

For example, in th below, comparing family A and B, A has a greater rank, so the nutriment with indicator ==1 corresponding to family A, now has a family == "independent".. and do it for all families.

Ive been really strugglign with this, and think it could resort t osome nested for loop? I have failed at implementing it and would lvoe anybody's help.

df1 <- data.frame(
  nutriment = c("glucose", "fructose", "lipid", "iron", "vitamin"),
  family = c("A", "B" , "B", "C", "D"),
  rank = c(1, 2 , 2, 3, 4),
  indicator = c(1, 1, 0, 1, 1)
)

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(data.table)
#> 
#> Attaching package: 'data.table'
#> The following objects are masked from 'package:dplyr':
#> 
#>     between, first, last

fams <- 
  df1 %>% 
    select(family, rank) %>% 
    distinct %>% 
    mutate(t = 1) %>% 
    left_join(., ., by = 't') %>% 
    filter(family.x != family.y, 
             rowid(paste(pmin(family.x, family.y), pmax(family.x, family.y))) == 1) %>% 
    mutate(gt = rank.y > rank.x) %>% 
    group_by(family.x, family.y) %>% 
    mutate(newdf = 
             df1 %>% 
              filter(family %in% c(family.x, family.y)) %>% 
              mutate(family = if_else(family == family.x & gt & indicator, 
                                      'independent', family)) %>% 
             list
    )
  
    
fams$newdf
#> [[1]]
#>   nutriment      family rank indicator
#> 1   glucose independent    1         1
#> 2  fructose           B    2         1
#> 3     lipid           B    2         0
#> 
#> [[2]]
#>   nutriment      family rank indicator
#> 1   glucose independent    1         1
#> 2      iron           C    3         1
#> 
#> [[3]]
#>   nutriment      family rank indicator
#> 1   glucose independent    1         1
#> 2   vitamin           D    4         1
#> 
#> [[4]]
#>   nutriment      family rank indicator
#> 1  fructose independent    2         1
#> 2     lipid           B    2         0
#> 3      iron           C    3         1
#> 
#> [[5]]
#>   nutriment      family rank indicator
#> 1  fructose independent    2         1
#> 2     lipid           B    2         0
#> 3   vitamin           D    4         1
#> 
#> [[6]]
#>   nutriment      family rank indicator
#> 1      iron independent    3         1
#> 2   vitamin           D    4         1

Created on 2021-05-18 by the reprex package (v2.0.0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM