R - 用同一 df 中的另一个值替换 if 循环中给定的某些语句的行值 [英] R - replace values by row given some statement in if loop with another value in same df
问题描述
我有一个数据集,我想用它进行多级分析.因此,我为每个患者设置了两行,还有一对带有 1 和 2 的列(1 = 患者,2 = 患者的伴侣).
I have a dataset with which I want to conduct a multilevel analysis. Therefore I have two rows for every patient, and a couple column with 1's and 2's (1 = patient, 2 = partner of patient).
现在,对于现在位于同一行的不同列中的患者和伴侣,我有带有出生日期和年龄的变量.我想要做的是编写一个代码:
Now, I have variables with date of birth and age, for both patient and partner in different columns that are now on the same row. What I want to do is to write a code that does:
if mydata$couple == 2, then replace mydata$dateofbirthpatient with mydata$dateofbirthpatient
对于每一行.由于我有多个要替换的变量,因此如果我能在循环中获取它并添加"要替换的变量,那就太好了.
And that for every row. Since I have multiple variables that I want to replace, it would be lovely if I could get this in a loop and just 'add' variables that I want to replace.
到目前为止我尝试过的:
What I tried so far:
mydf_longer <- if (mydf_long$couple == 2) {
mydf_long$pgebdat <- mydf_long$prgebdat
}
当然这是行不通的 - 只是简单地说这就是我想要的.
Ofcourse this wasn't working - but simply stated this is what I want.
我从这段代码开始,遵循 按行,替换指定列中等于值的值,但不知道如何完成:
And I started with this code, following the example in By row, replace values equal to value in specified column , but don't know how to finish:
mydf_longer[6:7][mydf_longer[,1:4]==mydf_longer[2,2]] <-
有什么想法吗?如果您需要更多信息,请与我们联系.
Any ideas? Let me know if you need more information.
数据示例:
# id couple groep_MNC zkhs fbeh pgebdat p_age pgesl prgebdat pr_age
# 1 3 1 1 1 1 1955-12-01 42.50000 1 <NA> NA
# 1.1 3 2 1 1 1 1955-12-01 42.50000 1 <NA> NA
# 2 5 1 1 1 1 1943-04-09 55.16667 1 1962-04-18 36.5
# 2.1 5 2 1 1 1 1943-04-09 55.16667 1 1962-04-18 36.5
# 3 7 1 1 1 1 1958-04-10 40.25000 1 <NA> NA
# 3.1 7 2 1 1 1 1958-04-10 40.25000 1 <NA> NA
mydf_long <- structure(
list(id = c(3L, 3L, 5L, 5L, 7L, 7L),
couple = c(1L, 2L, 1L, 2L, 1L, 2L),
groep_MNC = c(1L, 1L, 1L, 1L, 1L, 1L),
zkhs = c(1L, 1L, 1L, 1L, 1L, 1L),
fbeh = c(1L, 1L, 1L, 1L, 1L, 1L),
pgebdat = structure(c(-5145, -5145, -9764, -9764, -4284, -4284), class = "Date"),
p_age = c(42.5, 42.5, 55.16667, 55.16667, 40.25, 40.25),
pgesl = c(1L, 1L, 1L, 1L, 1L, 1L),
prgebdat = structure(c(NA, NA, -2815, -2815, NA, NA), class = "Date"),
pr_age = c(NA, NA, 36.5, 36.5, NA, NA)),
.Names = c("id", "couple", "groep_MNC", "zkhs", "fbeh", "pgebdat",
"p_age", "pgesl", "prgebdat", "pr_age"),
row.names = c("1", "1.1", "2", "2.1", "3", "3.1"),
class = "data.frame"
)
推荐答案
如果您只想根据条件更改值,以下 for
循环应该可以工作:
The following for
loop should work if you only want to change the values based on a condition:
for(i in 1:nrow(mydata)){
if(mydata$couple[i] == 2){
mydata$pgebdat[i] <- mydata$prgebdat[i]
}
}
或
正如@lmo 所建议的那样,以下操作会更快.
As suggested by @lmo, following will work faster.
mydata$pgebdat[mydata$couple == 2] <- mydata$prgebdat[mydata$couple == 2]
这篇关于R - 用同一 df 中的另一个值替换 if 循环中给定的某些语句的行值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!