1

I have this code:

edge_data$newly.exposedday1="No"
edge_data$newly.exposedday1= if (edge_data$Stats.day1=="E"& edge_data$from==Infected.Person) { 
    edge_data$newly.exposedday1==edge_data$to
    } else if (edge_data$Stats.day1=="E"& edge_data$to==Infected.Person) {
    edge_data$newly.exposedday1=edge_data$from
} 

This is the dataset:

edge_data <- structure(list(time_start = c(1, 1, 1, 1, 1, 1), time_end = c(2, 2, 2, 2, 2, 2), Person.1 = c(1558L, 1560L, 1567L, 1632L, 1632L, 1673L), Person.2 = c(1567L, 1570L, 1574L, 1818L, 1866L, 1698L ), attrs = c("3B-3B", "3B-3B", "3B-3B", "4B-4B", "4B-4B", "1B-1B" ), temp_id = c(1L, 1L, 1L, 1L, 1L, 1L), temp_ing = c(1, 1, 1, 1, 1, 1), from = c(59L, 60L, 64L, 86L, 86L, 103L), to = c(64L, 65L, 67L, 191L, 215L, 116L), Stats.day1 = c("Susceptible", "Susceptible", "Susceptible", "Susceptible", "Susceptible", "Susceptible")), row.names = c(NA, 6L), class = "data.frame") 

  time_start time_end Person.1 Person.2 attrs temp_id temp_ing from  to  Stats.day1
1          1        2     1558     1567 3B-3B       1        1   59  64 Susceptible
2          1        2     1560     1570 3B-3B       1        1   60  65 Susceptible
3          1        2     1567     1574 3B-3B       1        1   64  67 Susceptible
4          1        2     1632     1818 4B-4B       1        1   86 191 Susceptible
5          1        2     1632     1866 4B-4B       1        1   86 215 Susceptible
6          1        2     1673     1698 1B-1B       1        1  103 116 Susceptible

What I want it to do is change the edge_data$newlyexposedday1 column in the dataframe to either the ID in the from column, or the ID in the to column, and this would be determined by if the infected person was the to person, or the from person. In other words, I want the new column to have the person that wasn't the infected person. Any help would be appreciated. Thanks.

Alexis
  • 2,104
  • 2
  • 19
  • 40
Natalie
  • 13
  • 5

1 Answers1

2

Use ifelse since it is vectorized.

edge_data$newly.exposedday1 <- with(edge_data, ifelse(Stats.day1 =="E" & 
                        from %in% Infected.Person, to, ifelse(Stats.day1 == "E" & 
                           to %in% Infected.Person, from, 'No')))

If there are large number of conditions you can use case_when from dplyr :

library(dplyr)

edge_data %>%
  mutate(newly.exposedday1 = case_when(
      Stats.day1 =="E" & from %in% Infected.Person ~ as.character(to), 
      Stats.day1 == "E" & to %in% Infected.Person ~ as.character(from), 
      TRUE ~ "No"))

I am not sure if Infected.Person is a single number or multiple numbers, to be on the safer side I have used %in% which will work for both the cases.

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • 1
    Thanks Ronak, I think the only thing with that example is that it is not putting the persons ID in the column, also I don't think any of them changed. I'd like for the newly.exposedday1 to have the person's ID of the person who wasn't the infected person, so it should be what's in the edge_data$from column if the person was in the edge_data$to column, vice versa if the infected person was in the from column – Natalie Aug 02 '20 at 01:57
  • In steps, this is what I'd like for it to do: There is an infected person and that person will have IDs in both the to and from columns. I want for the newlyexposed column to look at only the rows that have "E" in the Stats.day1 column. If there is an E there, then look at the to and from columns and whichever does not have the ID of the infected person, I would like that ID to go into the newly.exposedday1 column – Natalie Aug 02 '20 at 02:01
  • Which is the person ID column in your data? Can you update your post to include expected output for the data shared? Give some value to `Infected.Person` and show what output you would expect so that it is clear. – Ronak Shah Aug 02 '20 at 02:02
  • is what I just wrote now helpful at all? Thanks so much for helping. – Natalie Aug 02 '20 at 02:03
  • If `from` and `to` columns consists of ID's that is what my code is doing. If `Stats.day1 =="E"` and `from` is in `Infected.Person` it returns `to` column value and vice versa. When none of `from` and `to` are infected it returns `"No"`. What output does it give when you run the code? What output do you expect? If you are using `dplyr` solution you need to assign the data back to see the changes. `edge_data <- edge_data %>% mutate(newly.exposedday1.......` – Ronak Shah Aug 02 '20 at 02:15
  • I wasn't doing, edge_data <- edge_data %>% mutate(newly.exposedday1...., thank you so much! – Natalie Aug 02 '20 at 02:21