简体   繁体   中英

R: Find a value in rows met a condition, add certain numbers to the value, and then create a message in that row in another column

I want to do something in R, which I believe there should be a way of doing it but I cannot figure it out.

What I want to do is finding a value in a column based on a message of another column, add numbers to the value, and then created a new column.

a <- c(1, 2, 3, 4, 5, 6, 7, 8)
b <- c(NA, "START", NA, NA, NA, NA, NA, NA)
df <- data.frame(a, b)

For example, I want to find a value in column 'a' when 'START' was presented in column B. It will be 2. And then, add 3 to the value, and create a column 'c' in which there is a message of 'STOP' in that row (when there is 5 in column 'a').

So I want the result to be like below.

  a     b    c
1 1  <NA> <NA>
2 2 START <NA>
3 3  <NA> <NA>
4 4  <NA> <NA>
5 5  <NA> STOP
6 6  <NA> <NA>
7 7  <NA> <NA>
8 8  <NA> <NA>

I have 12 trials per participants and there are more than 80 participants in total. I want to create "STOP" message in each trial by participants.

I was thinking to use group_by and mutate functions, but obviously it did not work.

library(dplyr)

df <- df %>%
      group_by(Participant, Trial) %>%
      mutate(time = df[df$b == "START","a"],
             stop = time + 3,
             c = case_when(df$time == stop ~ "STOP"))

Any insight/suggestions would be greatly appreciated!

You can try:

library(dplyr)

df %>%
  mutate(c = NA) %>%
  group_by(Participant, Trial) %>%
  mutate(c = replace(c, {inds <- which(b == 'START') + 3;inds[inds <= n()]}, 'STOP'))

We can use lag from dplyr as follows. It requires your column a being complete (no missing like 1, 2, 4, ...) and in order.

library(dplyr)

df %>%
  mutate(c = ifelse(lag(b, 3) %in% "START", "STOP", NA))
#   a     b    c
# 1 1  <NA> <NA>
# 2 2 START <NA>
# 3 3  <NA> <NA>
# 4 4  <NA> <NA>
# 5 5  <NA> STOP
# 6 6  <NA> <NA>
# 7 7  <NA> <NA>
# 8 8  <NA> <NA>

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