相关疑难解决方法(0)

根据另一列中的4个值创建新列

我想基于另一列中的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,但似乎没有工作.谢谢

if-statement r

25
推荐指数
3
解决办法
5万
查看次数

使用for和if基于数据框中的其他列添加列值

我有这样的数据帧:

     id        adit     diag1   diag2       
      2       3         4230    2234        
      3       5         3345    4456        
      4       6         4567    4467
Run Code Online (Sandbox Code Playgroud)

我想添加其他2列,dse1dse2使用下面的伪代码:

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)

但这些都行不通.

for-loop if-statement r

15
推荐指数
3
解决办法
7万
查看次数

标签 统计

if-statement ×2

r ×2

for-loop ×1