0

I have a big Excel table, which has information about different departments - mostly Dates. What I've done til yet, was to extract the dates manually and calculate with them. It works pretty well but it is hard coded:

#    Departement 1
aufnahmediagnose_days_abteilung1 <- as.Date(as.character(dat$ad_first[1:2]), format="%Y-%m-%d") - as.Date(as.character(dat$ad_occ[1:2]), format="%Y-%m-%d")
aufnahmediagnose_days_abteilung1 <- na.omit(aufnahmediagnose_days_abteilung1)
aufnahmediagnose_days_abteilung1 <- as.numeric(sum(aufnahmediagnose_days_abteilung1) / length(aufnahmediagnose_days_abteilung1))
#    Dempartment 2
aufnahmediagnose_days_abteilung2 <- as.Date(as.character(dat$ad_first[3:5]), format="%Y-%m-%d") - as.Date(as.character(dat$ad_occ[3:5]), format="%Y-%m-%d")
aufnahmediagnose_days_abteilung2 <- na.omit(aufnahmediagnose_days_abteilung2)
aufnahmediagnose_days_abteilung2 <- as.numeric(sum(aufnahmediagnose_days_abteilung2) / length(aufnahmediagnose_days_abteilung2))

Like you can see, I am using the hardcoded index, f.e. [3:5] (because this is one of the departments that I am looking for). The difference between the first and the second department is the information in the first column (record_id), like you can see in the following head of the table:

record_id redcap_event_na… department his   has_dob has_sex has_ad has_hd has_icdnd
      <dbl> <chr>                 <dbl> <chr>   <dbl>   <dbl>  <dbl>  <dbl>     <dbl>
1         1 fall_1_arm_1              1 Orbis       1       1      1      1         1
2         1 fall_2_arm_1             NA NA         NA      NA     NA     NA        NA
3         2 fall_1_arm_1             NA NA         NA      NA     NA     NA        NA
4         2 fall_2_arm_1             NA NA         NA      NA     NA     NA        NA
5         2 fall_3_arm_1             NA NA         NA      NA     NA     NA        NA

The columns that i need (ad_first and ad_occ) are not visible here, because I just display the head. The first 2 rows got the RecordID 1.

How can I choose the exact rows from ad_first and ad_occ, when the condition is, that I want to have the rows, where the RecordID equals 1?

double-beep
  • 5,031
  • 17
  • 33
  • 41
DodgeRand
  • 1
  • 1
  • 1
    Does this answer your question? [Select rows from a data frame based on values in a vector](https://stackoverflow.com/questions/11612235/select-rows-from-a-data-frame-based-on-values-in-a-vector) Or you can use `==` in the same way of `%in%` in the answer, with one value. – s__ Jan 27 '20 at 13:54
  • There are so many ways to extract a subset of rows based on a vector, as long as that vector is derived directly from the frame or is the same length as the number of rows in the frame. The simplest is demoed with `subset(mtcars, cyl < 5)`, where `cyl` is a column within the `mtcars` dataset; this is analogous to `mtcars[mtcars$cyl < 5,]`. Other packages provide similar functionality with `dplyr::filter(mtcars, cyl < 5)` and `data.table::as.data.table(mtcars)[cyl < 5,]`. Welcome to R and StackOverflow! – r2evans Jan 27 '20 at 16:14
  • Thanks guys! The other thread really helped me. Sorry for not finding it – DodgeRand Feb 03 '20 at 10:32

0 Answers0