Get value from other column based on value of a column

浪尽此生 提交于 2020-01-11 13:12:04

问题


For each row in a data frame I want to copy a value from one column to another depending on a value in third column.

I tried to do it with a combined for loop and if function:

    ## example

condition <- c("1","2","2","1","2","","3","3")
SZ01 <- c("1","1","1","1","1","","1","1")
SZ02 <- c("2","2","2","2","2","","2","2")
SZ03 <- c("3","3","3","3","3","","3","3")


df <- data.frame(cbind(condition,SZ01,SZ02,SZ03), stringsAsFactors = FALSE)


df$retribution <- NULL
df$special_prevention <- NULL
df$general_prevention <- NULL


for (i in 1:length(df$condition)){ 

  if (df$condition[i] == 1){

    df$retribution[i] <- df$SZ01[i]
    df$special_prevention[i] <- df$SZ02[i]
    df$general_prevention[i] <- df$SZ03[i]


  }else if (df$condition[i] == 2) {

    df$retribution[i] <- df$SZ02[i]
    df$special_prevention[i] <- df$SZ03[i]
    df$general_prevention[i] <- df$SZ01[i]

  }else if (df$condition[i] == 3) {

    df$retribution[i] <- df$SZ03[i]
    df$special_prevention[i] <- df$SZ01[i]
    df$general_prevention[i] <- df$SZ02[i]

  } else {
    df$retribution[i] <- "missing_condition"
    df$special_prevention[i] <- "missing_condition"
    df$general_prevention[i] <- "missing_condition"

  }
}

This seems to work (no error message), but looking at my data there must be something wrong.

For example row 1 has condition == 1. This means, that for this row df$retribution should receive the same value as the one in row 1 of df$SZ01.

But it doesn't. Is there anyone that can see the mistake I made?


回答1:


Try the following:

condition <- c("1","2","2","1","2","","3","3")
SZ01 <- c("1","1","1","1","1","","1","1")
SZ02 <- c("2","2","2","2","2","","2","2")
SZ03 <- c("3","3","3","3","3","","3","3")


df <- data.frame(cbind(condition,SZ01,SZ02,SZ03), stringsAsFactors = F)

df$retribution <- ifelse(df$condition == "1", df$SZ01,
                         ifelse(df$condition == "2", df$SZ02,
                            ifelse(df$condition == "3", df$SZ03, "missing")))

df$special_prevention <- ifelse(df$condition == "1", df$SZ02, 
                                ifelse(df$condition == "2", df$SZ03,
                                       ifelse(df$condition == "3", df$SZ01, "missing")))

df$general_prevention <- ifelse(df$condition == "1", df$SZ03, 
                                ifelse(df$condition == "2", df$SZ01,
                                       ifelse(df$condition == "3", df$SZ02, "missing"))))

Output:

df
  condition SZ01 SZ02 SZ03 retribution special_prevention general_prevention
1         1    1    2    3           1                  2                  3
2         2    1    2    3           2                  3                  1
3         2    1    2    3           2                  3                  1
4         1    1    2    3           1                  2                  3
5         2    1    2    3           2                  3                  1
6                              missing            missing            missing
7         3    1    2    3           3                  1                  2
8         3    1    2    3           3                  1                  2


来源:https://stackoverflow.com/questions/39104617/get-value-from-other-column-based-on-value-of-a-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!