我想基于另一列中的4个值创建一个新列.
if col1=1 then col2= G;
if col1=2 then col2=H;
if col1=3 then col2=J;
if col1=4 then col2=K.
Run Code Online (Sandbox Code Playgroud)
我怎么做R?我需要有人帮忙解决这个问题.我试过if/else和ifelse,但似乎没有工作.谢谢
我有这样的数据帧:
id adit diag1 diag2
2 3 4230 2234
3 5 3345 4456
4 6 4567 4467
Run Code Online (Sandbox Code Playgroud)
我想添加其他2列,dse1并dse2使用下面的伪代码:
if diag1 contains 4230 then dse1 = 1 else dse1 = 0
if diag2 contains 4567 then dse2 =1 else dse2 = 0
Run Code Online (Sandbox Code Playgroud)
我用过这个:
for (i in 1 : nrow(dse)){
for (j in 3: ncol(dse)){
if dse[i,j] %in% ("4320"){dse$dse1 = 1}
else{dse$dse1 = 0}
if dse[i,j] %in% ("4567"){dse$dse2 = 1}
else{dse$dse2 = 0}
}
}
Run Code Online (Sandbox Code Playgroud)
但这些都行不通.