Home > Back-end >  If function - replace by a formula
If function - replace by a formula

Time:11-13

On R: I have a dataset of 150 000 obs. of 645 variables. I need to convert all negative value of the dataset (645 columns) by a formula. Some column are string but most of the variables are numeric. Like: if value positive, stay the same AND if its negative i want to apply this formula (value *-1)/2

My dataset name is sed_lac_qc: I tried this:

if (sed_lac_qc<0 {sed_lac_qc=((sed_lac_qc*-1)/2)} else {sed_lac_qc=sed_lac_qc}

but it make this error: Warning message: In if (sed_lac_qc < 0) { : the condition has length > 1 and only the first element will be used

CodePudding user response:

Assume that we have a data frame in which every column is numeric. For example assume the data frame, BOD2, defined in the Note at the end based on the BOD data frame that comes with R.

1) lapply Then for each column perform the operation creating a list of columns and convert it back into a data frame. We can replace the indicated function with any other function as long as it represents an operation which is vectorized in R.

No packages are used.

as.data.frame(lapply(BOD2, function(x) ifelse(x > 0, x, -x/2)))

giving:

  Time demand
1  0.5    8.3
2  0.5   10.3
3  3.0   19.0
4  4.0   16.0
5  5.0   15.6
6  7.0   19.8

If there are non-numeric columns then use:

as.data.frame(lapply(BOD2, function(x) {
  if (is.numeric(x)) ifelse(x > 0, x, -x/2) else x
}))

2) pmax Alternately for this particular transformation we can use pmax giving the same result.

pmax(BOD2, -BOD2/2)

or if there are non-numeric columns then

is_num <- sapply(BOD2, is.numeric)
BOD3 <- BOD2[is_num]
replace(BOD2, is_num, pmax(BOD3, -BOD3/2))

3) dplyr Using dplyr we can do this:

library(dplyr)
BOD2 %>% mutate(across(where(is.numeric), ~ ifelse(. > 0, ., -./2)))

4) collapse We can use ftransformv from the collapse package:

library(collapse)
ftransformv(BOD2, is.numeric, function(x) ifelse(x > 0, x, -x/2))

Note

BOD2 <- BOD
BOD2[1:2, 1] <- -1

so BOD2 looks like this:

  Time demand
1   -1    8.3
2   -1   10.3
3    3   19.0
4    4   16.0
5    5   15.6

CodePudding user response:

Since you have a whole dataframe as numeric. Just do a mathematical computation on it:

sg <- df > 0
df1 <- df^sg * (-df/2)^(1-sg)

Another option is to use the ifelse:

data.frame(ifelse(df > 0, df, -df/2))

CodePudding user response:

As you have mentioned that you want to find out negative value from a data set and then want to convert negative value by a formula. If you have shared your code that you tried to solve this problem, then it will be clear to us that in which level you have reached. Moreover, I'm giving you a simple solution to this problem.

    if(value < 0){
        value = (value *-1)/2
    }else{
        value = value
    }

or try ifelse

ifelse(test, yes, no)

like:

ifelse(sed_lac_qc<0, (sed_lac_qc*-1)/2, sed_lac_qc)

Try to check your value is smaller than 0, if smaller than 0, then apply your formula.

  • Related