根据列的值从其他列中获取值 [英] Get value from other column based on value of a column
问题描述
对于数据框中的每一行,我都想根据第三列中的值将一列中的值复制到另一列。
我试着用
##示例
条件< - c( 1,2,2,1,2,,3,3)
SZ01 <-c(1,1,1 1,1,1,,1,1)
SZ02 <-c(2,2,2,2,2 (3,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 $条件)){
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]
}否则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
}
}
这似乎工作(没有错误信息),但看我的数据必须有一些错误的。
例如,行1有 condition == 1
。这意味着对于这一行 df $ retribution
应该得到与 df $ SZ01
。
但事实并非如此。有没有人可以看到我犯的错误?
请尝试以下操作:
$ b (1,2,2,1,2,,3,3 )
SZ01 < - c(1,1,1,1,1,,1,1)
SZ02 < (2,2,2,2,2,,2,2)
SZ03 < - c (条件,SZ01,...,3,3,3,,3,3)bb
df < - data.frame 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,缺少)))
df $ general_prevention< - ifelse(df $ condition ==1,df $ SZ03,
ifelse(df $ condition ==2,df $ SZ01,
ifelse(df $ condition ==3,df $ SZ02,missing)))
$ c $输出:
df $ b $输出:
条件SZ01 SZ02 SZ03报复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失踪失踪失踪
7 3 1 2 3 3 1 2
8 3 1 2 3 3 1 2
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?
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
这篇关于根据列的值从其他列中获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!