Rahul
Rahul

Reputation: 5

Replace row values with multiple conditions in r

Currently Im working on a project and got stuck in one problem. I have to replace row values of a column with two conditions in different columns. Suppose:

x   y     m     n
1  200P  Jan  Perm
1  200T  Feb  Temp  
1  300P  Jan  Perm
2  200T  Feb  Temp      
2  300T  Feb  Temp
3  300P  Jan  Perm
3  400P  Jan  Perm

I would like to change the values of column n based on x and y.

for each x
check the value of y and n, if the first value of y with T is 
Perm/Temp. Replace the rest of the values of unique x rows to that 
value.

I tried but when I execute the code it replaces all Temp to Perm or Perm to Temp. But I want it to change just the values of the rows for that unique x. Can someone please help me with this. I want my output to be like:

x   y     m     n
1  200P  Jan  Temp
1  200T  Feb  Temp  
1  300P  Jan  Temp
2  200T  Feb  Temp      
2  300T  Feb  Temp
3  300P  Jan  Perm
3  400P  Jan  Perm

I was trying to practice with another dataset with different conditions. For example:

a   b    c       d
1   1   0.4    Minor
1   1   0.4    Minor
1   4   0.2    Minor
1   2   2.4    Major
2   4   0.2    Minor
3   1   0.4    Minor
3   4   0.2    Minor
3   4   4.2    Major

I'm trying to replace 4 to 1 in column b with a condition that if it is 0.2 in column c. If 4 and 0.4 are in the same row, replace 4 to 1.

Upvotes: 1

Views: 1642

Answers (3)

Rui Barradas
Rui Barradas

Reputation: 76651

I believe the following code does what you want.
It creates a new column, n2, with the values of n corresponding to the first occurence of a T in y.

fun <- function(DF){
    i <- grep("T", DF$y)[1]
    DF$n2 <- DF$n
    if(!is.na(i)) DF$n2[seq_len(nrow(DF))[-seq_len(i - 1)]] <- DF$n[i]
    DF$n2
}

res <- dat    # work with a copy
res$n2 <- unlist(lapply(split(dat[c(1:2, 4)], dat$x), FUN = fun))
res
#  x    y   m    n   n2
#1 1 200P Jan Perm Perm
#2 1 200T Feb Temp Temp
#3 1 300P Jan Perm Temp
#4 2 200T Feb Temp Temp
#5 2 300T Feb Temp Temp
#6 3 300P Jan Perm Perm
#7 3 400P Jan Perm Perm

If you don't want that new column, just do

res$n <- res$n2
res <- res[-ncol(res)]

EDIT.

Apparently my original code was right. The following is what the OP asks for in the last comment.

fun2 <- function(DF){
    i <- grep("T", DF$y)[1]
    DF$n2 <- if(!is.na(i)) DF$n[i] else DF$n
    DF$n2
}

res2 <- dat    # work with a copy
res2$n2 <- unlist(lapply(split(dat[c(1:2, 4)], dat$x), FUN = fun))
res2
#  x    y   m    n   n2
#1 1 200P Jan Perm Temp
#2 1 200T Feb Temp Temp
#3 1 300P Jan Perm Temp
#4 2 200T Feb Temp Temp
#5 2 300T Feb Temp Temp
#6 3 300P Jan Perm Perm
#7 3 400P Jan Perm Perm

DATA.

dat <- read.table(text = "
x   y     m     n
1  200P  Jan  Perm
1  200T  Feb  Temp  
1  300P  Jan  Perm
2  200T  Feb  Temp      
2  300T  Feb  Temp
3  300P  Jan  Perm
3  400P  Jan  Perm
", header = TRUE)

EDIT 2.

With the conditions in your question edit, it is much simpler, use a logical index.
Note that in your edit first you say to change column b value from 4 to if column c is 0.2 but then you say to change it if column c is 0.4. The code below uses 0.2.

inx <- dat2$b == 4 & dat2$c == 0.2
dat2$b[inx] <- 1

DATA 2.

dat2 <- read.table(text = "
a   b    c       d
1   1   0.4    Minor
1   1   0.4    Minor
1   4   0.2    Minor
1   2   2.4    Major
2   4   0.2    Minor
3   1   0.4    Minor
3   4   0.2    Minor
3   4   4.2    Major
", header = TRUE)

Upvotes: 1

MKR
MKR

Reputation: 20095

You can use dplyr::first to find the 1st occurrence of y having value with T and then replace all values of n with value from found row.

library(dplyr)

df %>% group_by(x) %>%
  mutate(n = ifelse(!is.na(first(grep("T$",y))), 
                            n[first(grep("T$",y))], n )) %>%
  as.data.frame()

#   x    y   m    n
# 1 1 200P Jan Temp
# 2 1 200T Feb Temp
# 3 1 300P Jan Temp
# 4 2 200T Feb Temp
# 5 2 300T Feb Temp
# 6 3 300P Jan Perm
# 7 3 400P Jan Perm

Data:

df <- read.table(text = 
"x   y     m     n
1  200P  Jan  Perm
1  200T  Feb  Temp  
1  300P  Jan  Perm
2  200T  Feb  Temp      
2  300T  Feb  Temp
3  300P  Jan  Perm
3  400P  Jan  Perm",
header = TRUE, stringsAsFactors = FALSE)

Upvotes: 0

akrun
akrun

Reputation: 887851

We could also try with data.table

library(data.table)
i1 <- setDT(df1)[, {i1 <- grepl("T$", y)
            if(any(i1)) .I[which.max(i1):.N] } , x]$V1

Or

i1 <- setDT(df1)[, .I[cumsum(grepl("T$", y))!=0], x]$V1
df1[i1, n := first(n), x]
df1
#   x    y   m    n
#1: 1 200P Jan Perm
#2: 1 200T Feb Temp
#3: 1 300P Jan Temp
#4: 2 200T Feb Temp
#5: 2 300T Feb Temp
#6: 3 300P Jan Perm
#7: 3 400P Jan Perm

data

df1 <- structure(list(x = c(1L, 1L, 1L, 2L, 2L, 3L, 3L), y = c("200P", 
"200T", "300P", "200T", "300T", "300P", "400P"), m = c("Jan", 
"Feb", "Jan", "Feb", "Feb", "Jan", "Jan"), n = c("Perm", "Temp", 
"Perm", "Temp", "Temp", "Perm", "Perm")), .Names = c("x", "y", 
"m", "n"), class = "data.frame", row.names = c(NA, -7L))

Upvotes: 1

Related Questions