如果两个 dfs 中的两个 "age" 变量在 R 中匹配,如何将新列添加到变量 "hr" 的 -ln 的数据框中?
How to add a new column to a data frame that is the -ln of the variable "hr" if the two "age" variables in the two dfs match in R?
我的目标是在 df1 中创建一个新列 "HoLj",如果 df1 中的相应年龄与 df2 中的年龄 2 匹配,则它是 df2 中 "hr" 的 -ln。
df1<- data.frame(age = c("1","2","4","5","7","8"), dif = c("y", "n", "y", "n","n","y")
df2<- data.frame(age2=c("1","2","3","4","5","6","7","8"),hr=c(56, 57, 23, 46, 45, 19, 21, 79)
我的目标是在 df1 中创建一个如下所示的新列:
age dif hoLj
1 y -ln(56)
2 n -ln(57)
4 y -ln(46)
5 n -ln(45)
7 n -ln(21)
8 y -ln(79)
谢谢!
我们可以做一个连接然后得到自然对数
library(dplyr)
left_join(df1, df2) %>%
mutate(hoLj = -log(hr)) %>%
select(-hr)
或 data.table
library(data.table)
setDT(df1)[df2, hoLj := -log(hr), on = .(age)]
df1
# age dif hoLj
#1: 1 y -4.025352
#2: 2 n -4.043051
#3: 4 y -3.828641
#4: 5 n -3.806662
#5: 7 n -3.044522
#6: 8 y -4.369448
我的目标是在 df1 中创建一个新列 "HoLj",如果 df1 中的相应年龄与 df2 中的年龄 2 匹配,则它是 df2 中 "hr" 的 -ln。
df1<- data.frame(age = c("1","2","4","5","7","8"), dif = c("y", "n", "y", "n","n","y")
df2<- data.frame(age2=c("1","2","3","4","5","6","7","8"),hr=c(56, 57, 23, 46, 45, 19, 21, 79)
我的目标是在 df1 中创建一个如下所示的新列:
age dif hoLj
1 y -ln(56)
2 n -ln(57)
4 y -ln(46)
5 n -ln(45)
7 n -ln(21)
8 y -ln(79)
谢谢!
我们可以做一个连接然后得到自然对数
library(dplyr)
left_join(df1, df2) %>%
mutate(hoLj = -log(hr)) %>%
select(-hr)
或 data.table
library(data.table)
setDT(df1)[df2, hoLj := -log(hr), on = .(age)]
df1
# age dif hoLj
#1: 1 y -4.025352
#2: 2 n -4.043051
#3: 4 y -3.828641
#4: 5 n -3.806662
#5: 7 n -3.044522
#6: 8 y -4.369448