Alexander Alexander - 18 days ago 5
R Question

Mutate and ifelse() fail when logical check tail&head values of the column

I have a data frame and I want to add new column

ex
by conditional checking
tail
and
head
values of the another column.If the condition is match, insert
ex1
or
ex2
to new column.
Here is what I mean

x = rep(c(seq(1,3),seq(2,3)),2)
grp=gl(2,5)
df <- data.frame(x,grp)


# x grp
# 1 1 1
# 2 2 1
# 3 3 1
# 4 2 1
# 5 3 1
# 6 1 2
# 7 2 2
# 8 3 2
# 9 2 2
# 10 3 2


I tried

library(dplyr)

dff <- df%>%
group_by(grp)%>%
mutate(ex=ifelse(tail(x,2)==c(2,3),tail(ex,2)="ex2"&head(ex,3)="ex1",NA))

>Error: unexpected '=' in:
"group_by(grp)%>%
mutate(ex=ifelse(tail(x,2)==c(2,3),tail(ex,2)="


expected output

# x grp ex
# 1 1 1 ex1
# 2 2 1 ex1
# 3 3 1 ex1
# 4 2 1 ex2
# 5 3 1 ex2
# 6 1 2 ex1
# 7 2 2 ex1
# 8 3 2 ex1
# 9 2 2 ex2
# 10 3 2 ex2


How can I do that?

Answer

We can try with data.table

library(data.table)
i1 <- setDT(df)[, .I[tail(seq_len(.N),2)] , grp]$V1
df[, ex:= "ex1"]
df[df[i1, .I[x==c(2,3)] , grp]$V1, ex := "ex2"]
df
#   x grp  ex
#1: 1   1 ex1
#2: 2   1 ex1
#3: 3   1 ex1
#4: 2   1 ex2
#5: 3   1 ex2
#6: 1   2 ex1
#7: 2   2 ex1
#8: 3   2 ex1
#9: 2   2 ex2
#10:3   2 ex2

Or using dplyr

library(dplyr)
df %>% 
   group_by(grp) %>%
   mutate(x1 = rep(c(0, 2,3), c(n()-2, 1, 1)), 
          ex = if_else(x1==x & row_number() %in% tail(row_number(), 2), "ex2", "ex1")) %>%
   select(-x1)
#      x    grp    ex
#   <int> <fctr> <chr>
#1      1      1   ex1
#2      2      1   ex1
#3      3      1   ex1
#4      2      1   ex2
#5      3      1   ex2
#6      1      2   ex1
#7      2      2   ex1
#8      3      2   ex1
#9      2      2   ex2
#10     3      2   ex2
Comments