r - R:使用数据帧作为另一个数据帧的参数编写函数
问题描述
我正在用 R 编写一个函数,其中包含两个数据帧,但我需要一些帮助。我有两张表,一张是儿童低血压的临界值,一张是我的病人的大数据库。它们看起来像这样:
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)
我想知道我数据库中的患者是否患有低血压,并试图为其创建一个函数,但还不能创建一个有效的函数。
到目前为止,我的函数如下所示:
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")}}
我知道这不起作用,我需要找到一种方法,让 R 为每个患者查看数据库中的年龄,在我的低血压表中找到相同的年龄,并为患者的血压使用特定的截止值,但我没有线索如何做到这一点。任何人都可以帮助我,任何帮助将不胜感激!
解决方案
首先:熟悉包 dplyr,它非常适合数据处理。我在下面使用了它的一些操作。
我稍微更改了您的数据,以便构建一个工作示例。
# 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))
由于数据库有患者 ID,我了解您希望将这些患者的值与您的低血压数据库进行比较。我一步一步地完成它,所以很容易理解我在做什么。然后,您可以根据需要使用它围绕它构建一个函数。
# 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
希望这可以帮助!
推荐阅读
- javascript - 按距离给定的升序对点数组进行排序
- c# - CheckPasswordASync 总是返回 false
- python - 在等待方法tkinter中等待一秒钟
- c++ - 如何在 C++ 中修复“没有匹配的函数来调用...”?
- python - 用于匹配 Python 列表中的特定模式的正则表达式
- amazon-web-services - S3 向用户授予对象访问权限
- laravel - Vanilo 的自定义产品属性放在哪里?
- sass - 使用设计系统作为源在各自的项目目录中编译 sass 文件
- android - 如何将视图相对于非同级视图居中?
- r - 如何根据 R 中的预测变量和真实值计算准确度