Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 基于其他几列添加列及其值_R - Fatal编程技术网

R 基于其他几列添加列及其值

R 基于其他几列添加列及其值,r,R,我有这样的数据: id diag1 diag2 diag3 diag4 diag5 diag5 diag 6 diag7 diag8 diag9 26 V3000 75261 V053 V290 23 V3001 75261 V053 24 V3000 75537 75567

我有这样的数据:

id      diag1   diag2   diag3  diag4    diag5   diag5 diag 6     diag7  diag8    diag9

26  V3000   75261   V053    V290                                
23  V3001   75261   V053                                    
24  V3000   75537   75567   V053                                
19  V3001   7503    7613    7746    7631    7560    V290    76529   V1819           
29  V3001   77989   7470    7852    V053                            
31  V3000   75261   79415   77989   V053                            
33  V3000   7700    75329   7705    7750    7706    77089   7746    7661    75251     
20  V3000   7530    7795    76529   V053    V183                        
17  V3000   75329   7788    V053                                
22  4659    7862    7455    V7285                               
21  V3000   7503    77181   7579    7560    75251                       
30  V3000   7470    V053                                    
27  V3000   76519   7470    7726    7746    76719   76528   V053    V502    
我想添加var d1-d40,其值基于:

如果从diag1到diag9有“75261”,则d1=1,否则d1=0

如果从diag1到diag9有“7700”,则d2=1,否则d2=0

如果从diag1到diag9具有“7613”“75329”,则d3=1,否则d3=0

如果从diag1到diag9有'7470','7746',则d4=1 e;se d4=0 等

我使用了这样的代码

 bd$d40 = 0
 for (i in ncol(bd){
   if (bd[,i]  %in% ('75261')) {bd[,'d40'] = 1}
}
但他们没有工作。
谢谢。

我觉得您好像在试图确定给定行是否包含特定id。您可以使用apply()函数执行此操作:

d1 <- apply(bd, 1, function(x) as.numeric("75261" %in% x))
d2 <- apply(bd, 1, function(x) as.numeric("7700" %in% x))
...

d1我仍然记得当我意识到SAS表达式都有一个隐式for循环时,它将在执行时运行(仅在当前数据集中)。R代码可以被构建成做同样的事情,但是需要一个明确的行范围,以使矢量化正常工作,并在工作区中的所有项中正确地分配给特定的目标集

这可能会使其中一个for循环正常工作:

bd$d40 = 0
for (i in 2:10 ) {
    bd$d40 <- ifelse ( bd[,i]  %in% '75261',  1, bd$d40) 
}

您的代码有几处错误:1)在
for
语句中缺少
。2) 您在
语句中的
顺序错误。也许你是想做
ifelse
=
?3) 您的循环将在每次迭代中更改整个
d40
列。那没道理。嗨,欢迎来到SO!因为你是新来的,所以,请花点时间阅读和阅读。干杯。如果d3可能有很多代码,bd$d3如果你想检查每行是否都有这两个代码,你需要bd$d3谢谢。非常好。我想检查行中是否有一个代码,或者是许多代码中的一个(例如total 10)。对于许多(10)种代码,有没有比|更好的代码呢。这些代码对于一个代码来说非常好,但对于很多代码来说就不是了,比如目标代码是c('7356','7500','550')。我的意思是,对于col 2:9,如果他们有任何一个代码,d40将被1处理。如果任何一个示例项都与该组值匹配,那么这将是一个更有用的测试。
  pany <- function(df, items)) {   # edited to allow match for > 1 item
                   apply(df, 1, function(row) length(intersect( row , items)) >= 1 )}
  pany(bd[,2:10], '75261')
 [1]  TRUE  TRUE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE
 [12] FALSE FALSE
bd$d40 <- as.numeric(pany(bd[,2:10], '75261'))
bd$d40
 [1] 1 1 0 0 0 1 0 0 0 0 0 0 0