Performing an operation based on values in an adjacent column

I have a data frame with two columns relevant to the task at hand; one is numeric, and the other contains single letter codes to denote things about the data; i.e., below method detection limit, above the highest calibration standard, etc.

Some of these codes require the performance of a mathematical operation to correct for things within the data, and I was wondering what to do in order to edit a column based on the values in another.

I'm assuming I'll need some kind of loop, and it may be easier to create a new column of calculated values than to edit the existing one. In that case, I'll need an else clause to just copy the plain number over when no manipulation is required.

I'm still relatively new to R, so mainly I need help coming up with the syntax for the loop as well as what type of loop (I was assuming an 'if' with lots of 'else', but that may be wrong).

Here is an example of my dataframe:

  wbid              STA year month day time depth param mastercode  result rcode xcode lab         mdl          pql dunits ds
1 2749A 21FLA   20020137 2014     7   1 1125   0.3   410        ALK  26.000               6 0.650000000  2.500000000      m fs
2 2749A 21FLA   20020137 2014     7   1 1125   0.3 32209      CHLAC   2.300               6 0.690000000  2.100000000      m fs
3 2749A 21FLA   20020137 2014     7   1 1125   0.3    80      COLOR 240.000               6 5.000000000 12.000000000      m fs
4 2749A 21FLA   20020137 2014     7   1 1125   0.3   600         TN   1.704     +         0                                 fs
5 2749A 21FLA   20020137 2014     7   1 1125   0.3   665         TP   0.170               6 0.005000000  0.050000000      m fs
6 2749A 21FLA   20020137 2014     7   1 1125   0.3 49901        TSS   2.000     I     I   6 2.000000000 10.000000000      m fs

Basically, when the columns 'rcode' and/or 'xcode' contain a U, I have to take (result*2)/sqrt(2), so I want to iterate through rcode, xcode, and result and, when necessary, update result (or populate a new column) with the mathematically transformed value, or when it's not U coded, leave the value the same or paste the old one in.

Thanks!

R is a vectorized language so using a loop for this kind of thing is not a good idea, you can learn how to transform your data as you described by reading this free ebook.

https://r4ds.had.co.nz/transform.html

If you need more specific help, please provide a proper REPRoducible EXample (reprex) illustrating your issue.

I have updated my post with an example from my dataframe

That is not what I meant by posting a reproducible example, the example data you have posted is not even copy/paste friendly, for future posts, I strongly encourage you to read the guide in the link I gave you, and at least try to provide a proper reproducible example. Since you are new here, this time I'm going to make the extra effort of parsing some sample data from the table you have posted. This is what I think you are trying to achieve.

library(dplyr)

# Sample data on a copy/paste friendly format, replace this with your own data frame
sample_df <- data.frame(
  stringsAsFactors = FALSE,
              wbid = c("2749A_21FLA","2749A_21FLA",
                       "2749A_21FLA","2749A_21FLA","2749A_21FLA","2749A_21FLA"),
               STA = c(20020137,20020137,20020137,
                       20020137,20020137,20020137),
              year = c(2014, 2014, 2014, 2014, 2014, 2014),
             month = c(7, 7, 7, 7, 7, 7),
               day = c(1, 1, 1, 1, 1, 1),
              time = c(1125, 1125, 1125, 1125, 1125, 1125),
             depth = c(0.3, 0.3, 0.3, 0.3, 0.3, 0.3),
             param = c(410, 32209, 80, 600, 665, 49901),
        mastercode = c("ALK", "CHLAC", "COLOR", "TN", "TP", "TSS"),
            result = c(26, 2.3, 240, 1.704, 0.17, 2),
             rcode = c(NA, NA, NA, "+", NA, "I"),
             xcode = c(NA, NA, "U", NA, NA, "I")
)

# Relevant code
sample_df %>% 
    mutate(result = if_else((rcode %in% "U" | xcode %in% "U"),
                            (result*2)/sqrt(2),
                            result))
#>          wbid      STA year month day time depth param mastercode   result
#> 1 2749A_21FLA 20020137 2014     7   1 1125   0.3   410        ALK  26.0000
#> 2 2749A_21FLA 20020137 2014     7   1 1125   0.3 32209      CHLAC   2.3000
#> 3 2749A_21FLA 20020137 2014     7   1 1125   0.3    80      COLOR 339.4113
#> 4 2749A_21FLA 20020137 2014     7   1 1125   0.3   600         TN   1.7040
#> 5 2749A_21FLA 20020137 2014     7   1 1125   0.3   665         TP   0.1700
#> 6 2749A_21FLA 20020137 2014     7   1 1125   0.3 49901        TSS   2.0000
#>   rcode xcode
#> 1  <NA>  <NA>
#> 2  <NA>  <NA>
#> 3  <NA>     U
#> 4     +  <NA>
#> 5  <NA>  <NA>
#> 6     I     I

Created on 2022-06-23 by the reprex package (v2.0.1)

Thanks for the effort; in many cases, there are thousands of data points to iterate through, what I posted is just the result of me doing head(); is there a way to take entire columns and make them vectors automatically since it wouldn't be realistic to manually type out the vectors?

You are not supposed to type the data, what I have posted as sample data is just for reproducibility purposes (again, read the guide so you can understand the concept), you are supposed to apply the code labeled as "#Relevant code" to your own data frame instead of the sample data I show in the example.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.