If/else to remove rows contain 0

Hi there!

I have a series of datasets that contain a log transformation. I am trying to create a linear model of these datasets and store the resulting coefficients into vectors, and I'm doing this by having R iterate through the datasets via a loop. However, in at least one of the datasets, there is an input of 0, which makes the function undefined, and then the whole code falls apart for the rest of the loop. I've created an if/else statement to remove the rows containing 0s in a particular column, but.... it's not working. I get the following errors:

Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : 
  NA/NaN/Inf in 'y'
In addition: Warning message:
In if (data.2$LIGHT_UW == 0) { :
  the condition has length > 1 and only the first element will be used

Could someone take a look and see what I'm doing wrong?

Here is my code:

if(data.2$LIGHT_UW == 0) {
  # remove any rows with a Light_UW value of 0
  data.0 <- data.2[-which(data.2$LIGHT_UW==0),]
  # create a linear model of the log-transformed FRLIGHT as a function of depth
  lm <- lm(data.0$ln_FRLIGHT~data.0$DEPTH)
  # Store the model information
  summary <- summary(lm)
  # store the r2 value
  r2 <- summary$r.squared
  # store the multiplier
  Kd <- summary$coefficients[2,1]
} else {
  # create linear model with the data as it is
  lm <- lm(data.2$ln_FRLIGHT~data.2$DEPTH)
  summary <- summary(lm)
  r2 <- summary$r.squared
  Kd <- summary$coefficients[2,1]
}

Here is the dataset containing the input of 0:

data.2 <- structure(list(SITE_ID = c("GBA20-10538", "GBA20-10538", "GBA20-10538", 
"GBA20-10538", "GBA20-10538", "GBA20-10538", "GBA20-10538", "GBA20-10538", 
"GBA20-10538", "GBA20-10538", "GBA20-10538", "GBA20-10538", "GBA20-10538", 
"GBA20-10538", "GBA20-10538", "GBA20-10538", "GBA20-10538", "GBA20-10538", 
"GBA20-10538", "GBA20-10538", "GBA20-10538", "GBA20-10538", "GBA20-10538", 
"GBA20-10538", "GBA20-10538", "GBA20-10538", "GBA20-10538", "GBA20-10538"
), CAST = c("DOWN", "DOWN", "DOWN", "DOWN", "DOWN", "DOWN", "DOWN", 
"DOWN", "DOWN", "DOWN", "DOWN", "DOWN", "DOWN", "DOWN", "UP", 
"UP", "UP", "UP", "UP", "UP", "UP", "UP", "UP", "UP", "UP", "UP", 
"UP", "UP"), LINE = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 
13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28
), DEPTH = c(0.1, 0.5, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 
20, 15, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0.5, 0.1), TEMPERATURE = c(20.6, 
20.6, 20.6, 20.6, 20.3, 18.5, 16.8, 15.8, 14.6, 14.5, 14.3, 13.7, 
10.5, 9.5, 9.5, 10.4, 12.8, 13.3, 13.5, 14, 14.6, 18, 19.6, 19.9, 
20.2, 20.2, 20.2, 20.2), DO = c(11.2, 11.2, 11.1, 11.3, 11.2, 
11.1, 10.8, 10.5, 10.6, 10.5, 10.5, 10.5, 10.5, 10.7, 10.7, 10.5, 
10.6, 10.5, 10.5, 10.6, 10.6, 11.4, 11.3, 11.5, 11.3, 11.3, 11.2, 
11.2), PH = c(8.71, 8.71, 8.71, 8.71, 8.71, 8.66, 8.54, 8.47, 
8.36, 8.33, 8.29, 8.25, 8.13, 8.08, 8.08, 8.06, 8.08, 8.12, 8.13, 
8.17, 8.22, 8.47, 8.56, 8.61, 8.64, 8.67, 8.68, 8.67), CONDUCTIVITY = c(283.3, 
283.4, 283.5, 283.5, 282.8, 274.4, 274.1, 272.1, 272.1, 272, 
271.3, 268.4, 262.8, 260.4, 260.6, 265, 271.2, 271.4, 272.1, 
273.3, 275, 283.5, 285.2, 287.3, 285.4, 285.5, 285.1, 285.2), 
    LIGHT_AMB = c(2103.7, 2114.5, 2102.6, 2115.7, 2130, 2146.9, 
    2132.7, 2130.2, 2132.2, 2164, 2210.3, 2217.5, 2299.9, 2291.2, 
    2339, 1972.5, 1706.8, 2093, 1511.2, 1959.2, 1540.1, 2166.4, 
    2246.1, 2246.1, 2141, 2073.5, 2161, 2151.3), LIGHT_UW = c(1234.9, 
    1063.8, 848.3, 698, 231.7, 136.5, 73.8, 38.1, 28.3, 16.2, 
    9, 7, 1.9, 0, 0.4, 0.3, 6.9, 8.6, 13.3, 24, 44.8, 76.5, 135.5, 
    216.4, 377.7, 1092.4, 1117.8, 1288.9), FRLIGHT = c(0.587013357417883, 
    0.503097659021045, 0.403452867877865, 0.329914449118495, 
    0.108779342723005, 0.0635800456472123, 0.0346040230693487, 
    0.0178856445404187, 0.013272676109183, 0.00748613678373383, 
    0.00407184545084378, 0.00315670800450958, 0.000826122874907605, 
    0, 0.000171013253527148, 0.000152091254752852, 0.0040426529177408, 
    0.00410893454371715, 0.00880095288512441, 0.0122498979175174, 
    0.0290890201934939, 0.0353120384047267, 0.0603267886558924, 
    0.0963447753884511, 0.176412891172349, 0.526838678562817, 
    0.517260527533549, 0.599126109794078), ln_FRLIGHT = c(-0.532707704016207, 
    -0.686970974604326, -0.907695606328041, -1.1089219032269, 
    -2.21843382726514, -2.7554556055505, -3.36378532999712, -4.02375686887641, 
    -4.32204778486718, -4.89470239772803, -5.50365895456918, 
    -5.75822556512842, -7.09876703653877, -Inf, -8.67376949845833, 
    -8.79102985704596, -5.51085413971063, -5.49459151918654, 
    -4.73289528095959, -4.40223767529001, -3.53739448894348, 
    -3.34353134188547, -2.80797901758499, -2.33982211093242, 
    -1.73492805887127, -0.640860890086886, -0.659208609671288, 
    -0.512283169145905)), row.names = c(NA, -28L), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), groups = structure(list(SITE_ID = c("GBA20-10538", 
"GBA20-10538", "GBA20-10538", "GBA20-10538", "GBA20-10538", "GBA20-10538", 
"GBA20-10538", "GBA20-10538", "GBA20-10538", "GBA20-10538", "GBA20-10538", 
"GBA20-10538", "GBA20-10538", "GBA20-10538"), DEPTH = c(0.1, 
0.5, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20), .rows = structure(list(
    c(1L, 28L), c(2L, 27L), c(3L, 26L), c(4L, 25L), c(5L, 24L
    ), c(6L, 23L), c(7L, 22L), c(8L, 21L), c(9L, 20L), c(10L, 
    19L), c(11L, 18L), c(12L, 17L), c(13L, 16L), 14:15), ptype = integer(0), class = c("vctrs_list_of", 
"vctrs_vctr", "list"))), row.names = c(NA, -14L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE), na.action = structure(c(`1` = 1L), class = "omit"))

Any help would be greatly appreciated! Thanks so much!

This snippet takes advantage of the description of the data's origin to assume that any column containing a 0 on one row also contains a 0 on the another column in the same row and -Inf in a third column on the same row.

data.2 <- structure(list(
  SITE_ID = c(
    "GBA20-10538", "GBA20-10538", "GBA20-10538",
    "GBA20-10538", "GBA20-10538", "GBA20-10538", "GBA20-10538", "GBA20-10538",
    "GBA20-10538", "GBA20-10538", "GBA20-10538", "GBA20-10538", "GBA20-10538",
    "GBA20-10538", "GBA20-10538", "GBA20-10538", "GBA20-10538", "GBA20-10538",
    "GBA20-10538", "GBA20-10538", "GBA20-10538", "GBA20-10538", "GBA20-10538",
    "GBA20-10538", "GBA20-10538", "GBA20-10538", "GBA20-10538", "GBA20-10538"
  ), CAST = c(
    "DOWN", "DOWN", "DOWN", "DOWN", "DOWN", "DOWN", "DOWN",
    "DOWN", "DOWN", "DOWN", "DOWN", "DOWN", "DOWN", "DOWN", "UP",
    "UP", "UP", "UP", "UP", "UP", "UP", "UP", "UP", "UP", "UP", "UP",
    "UP", "UP"
  ), LINE = c(
    1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
    13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28
  ), DEPTH = c(
    0.1, 0.5, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20,
    20, 15, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0.5, 0.1
  ), TEMPERATURE = c(
    20.6,
    20.6, 20.6, 20.6, 20.3, 18.5, 16.8, 15.8, 14.6, 14.5, 14.3, 13.7,
    10.5, 9.5, 9.5, 10.4, 12.8, 13.3, 13.5, 14, 14.6, 18, 19.6, 19.9,
    20.2, 20.2, 20.2, 20.2
  ), DO = c(
    11.2, 11.2, 11.1, 11.3, 11.2,
    11.1, 10.8, 10.5, 10.6, 10.5, 10.5, 10.5, 10.5, 10.7, 10.7, 10.5,
    10.6, 10.5, 10.5, 10.6, 10.6, 11.4, 11.3, 11.5, 11.3, 11.3, 11.2,
    11.2
  ), PH = c(
    8.71, 8.71, 8.71, 8.71, 8.71, 8.66, 8.54, 8.47,
    8.36, 8.33, 8.29, 8.25, 8.13, 8.08, 8.08, 8.06, 8.08, 8.12, 8.13,
    8.17, 8.22, 8.47, 8.56, 8.61, 8.64, 8.67, 8.68, 8.67
  ), CONDUCTIVITY = c(
    283.3,
    283.4, 283.5, 283.5, 282.8, 274.4, 274.1, 272.1, 272.1, 272,
    271.3, 268.4, 262.8, 260.4, 260.6, 265, 271.2, 271.4, 272.1,
    273.3, 275, 283.5, 285.2, 287.3, 285.4, 285.5, 285.1, 285.2
  ),
  LIGHT_AMB = c(
    2103.7, 2114.5, 2102.6, 2115.7, 2130, 2146.9,
    2132.7, 2130.2, 2132.2, 2164, 2210.3, 2217.5, 2299.9, 2291.2,
    2339, 1972.5, 1706.8, 2093, 1511.2, 1959.2, 1540.1, 2166.4,
    2246.1, 2246.1, 2141, 2073.5, 2161, 2151.3
  ), LIGHT_UW = c(
    1234.9,
    1063.8, 848.3, 698, 231.7, 136.5, 73.8, 38.1, 28.3, 16.2,
    9, 7, 1.9, 0, 0.4, 0.3, 6.9, 8.6, 13.3, 24, 44.8, 76.5, 135.5,
    216.4, 377.7, 1092.4, 1117.8, 1288.9
  ), FRLIGHT = c(
    0.587013357417883,
    0.503097659021045, 0.403452867877865, 0.329914449118495,
    0.108779342723005, 0.0635800456472123, 0.0346040230693487,
    0.0178856445404187, 0.013272676109183, 0.00748613678373383,
    0.00407184545084378, 0.00315670800450958, 0.000826122874907605,
    0, 0.000171013253527148, 0.000152091254752852, 0.0040426529177408,
    0.00410893454371715, 0.00880095288512441, 0.0122498979175174,
    0.0290890201934939, 0.0353120384047267, 0.0603267886558924,
    0.0963447753884511, 0.176412891172349, 0.526838678562817,
    0.517260527533549, 0.599126109794078
  ), ln_FRLIGHT = c(
    -0.532707704016207,
    -0.686970974604326, -0.907695606328041, -1.1089219032269,
    -2.21843382726514, -2.7554556055505, -3.36378532999712, -4.02375686887641,
    -4.32204778486718, -4.89470239772803, -5.50365895456918,
    -5.75822556512842, -7.09876703653877, -Inf, -8.67376949845833,
    -8.79102985704596, -5.51085413971063, -5.49459151918654,
    -4.73289528095959, -4.40223767529001, -3.53739448894348,
    -3.34353134188547, -2.80797901758499, -2.33982211093242,
    -1.73492805887127, -0.640860890086886, -0.659208609671288,
    -0.512283169145905
  )
), row.names = c(NA, -28L), class = c(
  "grouped_df",
  "tbl_df", "tbl", "data.frame"
), groups = structure(list(SITE_ID = c(
  "GBA20-10538",
  "GBA20-10538", "GBA20-10538", "GBA20-10538", "GBA20-10538", "GBA20-10538",
  "GBA20-10538", "GBA20-10538", "GBA20-10538", "GBA20-10538", "GBA20-10538",
  "GBA20-10538", "GBA20-10538", "GBA20-10538"
), DEPTH = c(
  0.1,
  0.5, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20
), .rows = structure(list(
  c(1L, 28L), c(2L, 27L), c(3L, 26L), c(4L, 25L), c(5L, 24L), c(6L, 23L), c(7L, 22L), c(8L, 21L), c(9L, 20L), c(
    10L,
    19L
  ), c(11L, 18L), c(12L, 17L), c(13L, 16L), 14:15
), ptype = integer(0), class = c(
  "vctrs_list_of",
  "vctrs_vctr", "list"
))), row.names = c(NA, -14L), class = c(
  "tbl_df",
  "tbl", "data.frame"
), .drop = TRUE), na.action = structure(c(`1` = 1L), class = "omit"))

summary(data.2)
#>    SITE_ID              CAST                LINE           DEPTH       
#>  Length:28          Length:28          Min.   : 1.00   Min.   : 0.100  
#>  Class :character   Class :character   1st Qu.: 7.75   1st Qu.: 2.000  
#>  Mode  :character   Mode  :character   Median :14.50   Median : 5.500  
#>                                        Mean   :14.50   Mean   : 6.471  
#>                                        3rd Qu.:21.25   3rd Qu.: 9.000  
#>                                        Max.   :28.00   Max.   :20.000  
#>   TEMPERATURE          DO              PH         CONDUCTIVITY     LIGHT_AMB   
#>  Min.   : 9.50   Min.   :10.50   Min.   :8.060   Min.   :260.4   Min.   :1511  
#>  1st Qu.:13.65   1st Qu.:10.50   1st Qu.:8.160   1st Qu.:271.4   1st Qu.:2100  
#>  Median :16.30   Median :10.75   Median :8.470   Median :274.2   Median :2132  
#>  Mean   :16.33   Mean   :10.89   Mean   :8.422   Mean   :276.1   Mean   :2093  
#>  3rd Qu.:20.20   3rd Qu.:11.20   3rd Qu.:8.670   3rd Qu.:283.5   3rd Qu.:2177  
#>  Max.   :20.60   Max.   :11.50   Max.   :8.710   Max.   :287.3   Max.   :2339  
#>     LIGHT_UW         FRLIGHT          ln_FRLIGHT     
#>  Min.   :   0.0   Min.   :0.00000   Min.   :   -Inf  
#>  1st Qu.:   8.9   1st Qu.:0.00410   1st Qu.:-5.4969  
#>  Median :  59.3   Median :0.03185   Median :-3.4506  
#>  Mean   : 314.0   Mean   :0.14812   Mean   :   -Inf  
#>  3rd Qu.: 457.8   3rd Qu.:0.21479   3rd Qu.:-1.5784  
#>  Max.   :1288.9   Max.   :0.59913   Max.   :-0.5123
no_zeros <- data.2[which(data.2[,10] > 0),]
summary(no_zeros)
#>    SITE_ID              CAST                LINE           DEPTH      
#>  Length:27          Length:27          Min.   : 1.00   Min.   : 0.10  
#>  Class :character   Class :character   1st Qu.: 7.50   1st Qu.: 2.00  
#>  Mode  :character   Mode  :character   Median :15.00   Median : 5.00  
#>                                        Mean   :14.52   Mean   : 5.97  
#>                                        3rd Qu.:21.50   3rd Qu.: 8.50  
#>                                        Max.   :28.00   Max.   :20.00  
#>   TEMPERATURE          DO             PH         CONDUCTIVITY     LIGHT_AMB   
#>  Min.   : 9.50   Min.   :10.5   Min.   :8.060   Min.   :260.6   Min.   :1511  
#>  1st Qu.:13.85   1st Qu.:10.5   1st Qu.:8.195   1st Qu.:271.7   1st Qu.:2098  
#>  Median :16.80   Median :10.8   Median :8.470   Median :274.4   Median :2132  
#>  Mean   :16.59   Mean   :10.9   Mean   :8.435   Mean   :276.6   Mean   :2085  
#>  3rd Qu.:20.20   3rd Qu.:11.2   3rd Qu.:8.670   3rd Qu.:283.5   3rd Qu.:2165  
#>  Max.   :20.60   Max.   :11.5   Max.   :8.710   Max.   :287.3   Max.   :2339  
#>     LIGHT_UW          FRLIGHT            ln_FRLIGHT     
#>  Min.   :   0.30   Min.   :0.0001521   Min.   :-8.7910  
#>  1st Qu.:  11.15   1st Qu.:0.0057975   1st Qu.:-5.1946  
#>  Median :  73.80   Median :0.0346040   Median :-3.3638  
#>  Mean   : 325.59   Mean   :0.1536066   Mean   :-3.5688  
#>  3rd Qu.: 537.85   3rd Qu.:0.2531637   3rd Qu.:-1.4219  
#>  Max.   :1288.90   Max.   :0.5991261   Max.   :-0.5123

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.