Home > OS >  How to change values of R cells (dataframe) based on a condition for specific rows>?
How to change values of R cells (dataframe) based on a condition for specific rows>?

Time:05-08

I have the following dataframe,

C1 C2 C3
0 0 0
1 1 0
0 0 0
1 1 0
0 0 0

I want to now apply the following condition on the dataframe for specific indexes only.

  • C1 should be equal to 0
  • A random number should be less than 0.5

If the above conditions match, I want to change the value of the Cell in C1 and C2 to 1 else do nothing.

I am trying the following: (rowIndex is the specific indexes on which I want to apply the conditions)

apply(DF[rowsIndex,], 2, fun)

where fun is:

fun<- function(x) {
  ifelse(x==0,ifelse(runif(n=1)<0.5,x <- 1,x),x )
  print(x)
}

My questions are:

  • In my function, How do I apply the conditions to a certain column only i.e C1 (I have tried using DF[rowsIndex,c(1)], but gives an error
  • Is there any other approach I can take Since this approach is not giving me any results and the same DF is printed.

Thanks

CodePudding user response:

If you want to stay in base R:

fun <- function(x) {
  if(x[1]==0 & runif(n=1)<0.5) {
    x[1:2] <- 1
    }
  
  print(x)
}

#Using MARGIN = 1 applies the function to the rows of a dataframe
t(apply(DF[1:5,], 1, fun))

CodePudding user response:

Something like this?

library(dplyr)

df %>% 
  mutate(across(c(C1, C2), ~ifelse(C1 == 0 & runif(1) < 0.5, 1, .)))
   C1 C2 C3
1  1  0  0
2  1  1  0
3  1  0  0
4  1  1  0
5  1  0  0

Applying it to your function:

fun<- function(df, x, y) {
  df %>% 
    mutate(across(c({{x}}, {{y}}), ~ifelse({{x}} == 0 & runif(1) < 0.5, 1, .)))
}

fun(df, C1, C2)
  C1 C2 C3
1  0  0  0
2  1  1  0
3  0  0  0
4  1  1  0
5  0  0  0
  • Related