Rebecca Naeko Weber
Rebecca Naeko Weber

Reputation: 1

Select column that matches value in other column

I'm trying to extract the max temperature for the day that each data point burned in R. The first 365 columns in my data frame correspond to day of year (colnames '1', '2',...'365') and the last columns are identifiers for the data points, including day of burn (colname 'dob') which is listed as a day of the year (value = 1, 2, ...365). For each data point, I want to select the max temp value in the column that corresponds to the day of burn, e.g. if a data point burned on 227, then the max temp value in column 227 would be selected. Pretty new to coding, but this is generally how I'm thinking of it:

>dput(data[1:3, c(1, 225,226,227,365, 366, 374)])
    structure(list(`1` = c(645, 645, 645), `225` = c(900, 900, 900), 
    `226` = c(895, 895, 895), `227` = c(881, 881, 881), `365` = c(605, 
    605, 605), dob = c(227L, 222L, 222L), index = c("blossom_biscuit_1", 
    "blossom_biscuit_2", "blossom_biscuit_3")), row.names = c("1", 
    "2", "3"), class = "data.frame")

Current data structure:

 |    1...   | ...222... |  226 |   227...  | ...365| **dob** |      index      |
 |:---------:|:---------:|:----:|:---------:|:-----:|:-------:|:----------------|
 |     645   |    900    | 895  |  **881**  |  605  | **227** |blossom_biscuit_1|
 |     645   |  **900**  | 895  |    881    |  605  | **225** |blossom_biscuit_2|

Desired data structure:

 |  1...  | ...222... |226| 227...|...365| dob |       index      |**dob_tmmax**|
 |:------:|:---------:|:-:|:-----:|:----:|:---:|:-----------------|:-----------:|
 |  645   |     900   |895|  881  |  605 | 227 | blossom_biscuit_1|    881      |
 |  645   |     900   |895|  881  |  605 | 225 | blossom_biscuit_2|    900      |

Vague idea of for loop to accomplish this?

data %>% filter(
   for (i in data) {
                 dob = data$dob
                   x = c(data[i, dob])
          dob_tmmax <- c(data[i, x])
  return(data$dob_tmmax)  
})

Upvotes: 0

Views: 219

Answers (1)

akrun
akrun

Reputation: 887153

If the 'burn' column signifies the column index of the columns that have the max 'temp' value, an easier option is row/column indexing

df1$maxtemp <- df1[cbind(seq_len(nrow(df1)), df1$burn)]

Based on the update, we can use match to get the column index from the 'dob' column

nm1 <- grep('\\d+', names(data), value = TRUE)
dob_tmmax <- data[nm1][cbind(seq_len(nrow(data)), match(data$dob, nm1))]

Upvotes: 1

Related Questions