0

I'm writing a function in R with two dataframes in it, but I need some help. I have two tables, one with the cutoff values for hypotension in children and one big database of my patients. They look like this:

hypotension<-data.frame(age=c(1:18), boys=c(66,76,78,82,84....), girls=c(66,75,78,83, 85...)
database<-data.frame(patient_nr=c(1:1000), age=c(M,F,M,M,F...), gender, bloodpressure, heartrate, operation)

I want to know if the patients in my database have hypotension and tried to make a function for it, but couldn't make one that worked yet.

So far, my function looks like this:

got_hypotension <- function(hypotension, database){
X<-ifelse(database$gender=="M" & database$age==hypotension$age, print(hypotension$boys),
ifelse(database$gender=="F" & database$age==hypotension$age, print(hypotension$girls),
ifelse(database$age>18, print(90), 999)))
if(database$bloodpressure<X & !is.na(database$bloodpressure) { 
print("YES")}}

I understand that this doesnt work, I need to find a way that R, for each patient, looks at age in the database, find the same age in my hypotension table and use that specific cutoff value for the patients bloodpressure, but I have no clue how to do this. Can anybody help me, any help would be appreciated!

annas
  • 3
  • 1
  • Welcome on SO, please have a read here and try to enhance your question: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Terru_theTerror Sep 20 '18 at 09:39

1 Answers1

0

First off: get familiar with the package dplyr, it is great for data wrangling. I use a few of it's operations down below.

I changed your data a little bit so I could build a working example.

# data
hypotension<-data.frame(age=c(21:25), M=c(66,76,78,82,84), F=c(66,75,78,83,85))

database<-data.frame(patient_nr=c(1:10), age=c(21:30), gender = c("M", "F","M", "F","M", "F","M", "F","M","F"), 
                     bloodpressure = c(68, 78, 65, 65, 65, 65, 65, 60, 50, 50))

As the database has patient ID's, I understand you want to compare those patient's values with your hypotension database. I go through it step by step so it's easy to understand what I'm doing. You can then use that to build a function around it if you want.

# first, let's tidy your data. We change the hypotension database so that "age" and "gender" are columns.
# For this, you can use the gather function. you give it the names of the key and value column, followed by all the colums that you want to 'gather' on.
# The gather function then grabs those columns and transforms them into two rows. This is called a long format and is great for comparison and selection operations
hypotension_2 <- gather(hypotension, key = gender, value = bloodpressure_2, -age)

# Now let's change the names for gender so they match the database. This makes comparing easier
hypotension_2$gender[hypotension_2$gender == "boys"]<- "M"
hypotension_2$gender[hypotension_2$gender == "girls"]<- "F"

# now we just join the databases together, adding bloodpressure! joining tables is a quick and efficient operation.
DB_PB_join<- left_join(database, hypotension_2, by = c("gender", "age"))

# Now lets create a final column comparing the two bloodpressures. We can use mutate to create a new column
DB_PB_join %>% mutate(above_cutoff = if_else(bloodpressure > bloodpressure_2, T, F))

# as you can see, if the blood pressure doesn't match with anything in the table it returns NA. Otherwise, it returns true or false depending on the cutoff value. 
# You can fill the NA's with anything you want after

Hope this helps!