R - replace values by row given some statement in

2019-08-23 13:50发布

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.

Example of data:

#     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"
)

标签: r replace
1条回答
祖国的老花朵
2楼-- · 2019-08-23 14:51

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]
  }
}

OR

As suggested by @lmo, following will work faster.

mydata$pgebdat[mydata$couple == 2] <- mydata$prgebdat[mydata$couple == 2]
查看更多
登录 后发表回答