R ifelse unique loop always evaluates to FALSE

I am new to R and am having problems with a for loop over unique values.

with df:

id = c(1,2,2,3,3,4) 
rank = c(1,2,1,3,3,4) 
df = data.frame(id, rank)     

      

I am running:

df$dg <- logical(6)

for(i in unique(df$id)){
  ifelse(!unique(df$rank), df$dg ==T, df$dg == F)
}

      

I am trying to mark the variable $ dg as T, assuming the rank is different for each unique id, and F if the rank is the same in each id.

I don't get any errors, but I only get F for all $ dg values, although I should be getting the mix.

I also used the following loop with the same results:

for(i in unique(df$id)){
  ifelse(length(unique(df$rank)), df$dg ==T, df$dg == F)
}

      

I've read other similar posts but the advice didn't work for my case.

From comments:

I want to mark dg TRUE for all instances of an id, if the rank has changed at all for a given id. My point is that for a given id that has anywhere between 1-13 instances, mark dg TRUE if the rank is different from different instances.

+3


source to share


1 answer



Update: how to identify groups (ids) that only have one rank?


After figuring out that the OP provided this, there would be a solution for this specific case:

library(dplyr)
df %>% 
   group_by(id) %>% 
         mutate(dg = ifelse( length(unique(rank))>1 | n() == 1, T, F))

      

For another dataset that also has an ID that has duplicates but also non-double rank (presented below), this would be the output:

df2 %>% 
   group_by(id) %>% 
         mutate(dg = ifelse( length(unique(rank))>1 | n() == 1, T, F))

#:OUTPUT:

# Source: local data frame [9 x 3] 
# Groups: id [5] 
#  
# # A tibble: 9 x 3 
#      id  rank    dg 
#   <dbl> <dbl> <lgl> 
# 1     1     1  TRUE 
# 2     2     2  TRUE 
# 3     2     1  TRUE 
# 4     3     3 FALSE 
# 5     3     3 FALSE 
# 6     4     4  TRUE 
# 7     5     1  TRUE 
# 8     5     1  TRUE 
# 9     5     3  TRUE

      

Data-no-2:

df2 <- structure(list(id = c(1, 2, 2, 3, 3, 4, 5, 5, 5), rank = c(1, 2, 1, 3, 3, 4, 1, 1, 3
                )), .Names = c("id", "rank"), row.names = c(NA, -9L), class = "data.frame")

      




How to identify duplicate lines in each group (id)?




You can use the package : dplyr

library(dplyr)
df %>% 
   group_by(id, rank) %>% 
                      mutate(dg = ifelse(n() > 1, F,T))

      

This will give you:

# Source: local data frame [6 x 3] 
# Groups: id, rank [5] 
#  
# # A tibble: 6 x 3 
#      id  rank    dg 
#   <dbl> <dbl> <lgl> 
# 1     1     1  TRUE 
# 2     2     2  TRUE 
# 3     2     1  TRUE 
# 4     3     3 FALSE 
# 5     3     3 FALSE 
# 6     4     4  TRUE

      

Note. You can simply convert it back to data.frame()

.

A solution would be: data.table

dt <- data.table(df)
dt$dg <- ifelse(dt[ , dg := .N, by = list(id, rank)]$dg>1,F,T)

      

Data:

df <- structure(list(id = c(1, 2, 2, 3, 3, 4), rank = c(1, 2, 1, 3, 
      3, 4)), .Names = c("id", "rank"), row.names = c(NA, -6L), class = "data.frame")

# > df

#   id rank 
# 1  1    1 
# 2  2    2 
# 3  2    1 
# 4  3    3 
# 5  3    3 
# 6  4    4

      


N. B. Unless you need a different identifier instead TRUE/FALSE

, the usage ifelse()

is redundant and worth calculating. @DavidArenburg

+2


source







All Articles