多列的行计算

时间:2019-07-27 06:17:51

标签: r

R中是否可以这样做?我正在努力做到这一点。

对于每一行,都有LC1,LC2等。...已定义

Computation need to be done as follows for each row by row:

1. If F1=1, then SSN1=0, SSN2=0, SSN3=0 (If this condition is met then we should directly 
go to next row for computation, we dnt need to check for F2,F3)
     else  F1 not equal 1, then SSN1=SS1


If F1 was not equal 1 then we should proceed below
        Then it should check for if F2=1, then SSN2=0, SSN3=0 (If this condition is met then we should directly 
        go to next row for computation, we dnt need to check for F3)
            else F2 not equal 1 , then SSN2=SS2



If F2 was not equal 1 then we should proceed below
                Then it should check for if F3=1, then SSN3=0
                    else F3 not equal 1 , then SSN3=SS3

输入:

Item    LC1    SS1   F1    LC2   SS2  F2  LC3  SS3   F3
A123    MW1    20    1     SW1   10   2   RM1  10    2
A123    MW1    20    1     WK1   5    4   NA   NA    NA
B123    MW1    15    2     RS1   10   1   RM1  10    2
B123    MW1    15    2     RM1   10   2   RT1  5     1

输出:

Item    LC1    SS1   F1  SSN1   LC2   SS2  F2  SSN2  LC3  SS3   F3  SSN3
A123    MW1    20    1    0     SW1   10   7     0   RM1  10    2    0 
A123    MW1    20    1    0     WK1   5    4     0   NA   NA    NA   0
B123    MW1    15    2   15     RS1   10   1     0   M1   10    2    0
B123    MW1    15    2   15     RM1   10   2    10   RT1  5     1    0

1 个答案:

答案 0 :(得分:3)

目前我没有epochs = [10 15 20 25 30 35 40 45 10 15 20 25 30 35 40 45 10 15 20 25 30 35 40 45 10 15 20 25 30 35 40 45 10 15 20 25 30 35 40 45 10 15 20 25 30 35 40 45] ; L1=reshape(epochs,[8,6]); learning_rate=[0.002 0.002 0.002 0.002 0.002 0.002 0.002 0.002 0.003 0.003 0.003 0.003 0.003 0.003 0.003 0.003 0.004 0.004 0.004 0.004 0.004 0.004 0.004 0.004 0.005 0.005 0.005 0.005 0.005 0.005 0.005 0.005 0.006 0.006 0.006 0.006 0.006 0.006 0.006 0.006 0.007 0.007 0.007 0.007 0.007 0.007 0.007 0.007]; L2=reshape(learning_rate,[8,6]); rmse_abundance = [ 0.9904 0.9901 0.9799 0.9669 0.9561 0.9491 0.9428 0.9428 0.9900 0.9902 0.9667 0.9542 0.943 0.9357 0.9318 0.9267 1.0001 0.9668 0.9512 0.9393 0.932 0.9271 0.924 0.923 0.9813 0.9552 0.9429 0.9328 0.9285 0.9245 0.924 0.9213 0.9678 0.9465 0.9353 0.9284 0.9256 0.9235 0.9258 0.9245 0.9633 0.9424 0.9319 0.9281 0.9246 0.9249 0.9273 0.9263]; figure ; rmse_abu = reshape(rmse_abundance,[8,6]); bar3(rmse_abu) ; set(gca(gcf), 'yticklabel',{'10', '15' ,'20', '25', '30', '35' ,'40' ,'45'},'xticklabel',{'0.002', '0.003', '0.004', '0.005' ,'0.006','0.007','100' ,'500' ,'1e3', '5e3' ,'1e4'}); ylabel('epochs') ; xlabel ('gamma'); zlabel('rmse ') ; title('SGD'); (升级失败:-(),所以这是带有嵌套dplyr的base-R蛮力实现:

ifelse

还有其他几种工作方式,其中大多数更易于阅读。


x <- read.table(header=TRUE, stringsAsFactors=FALSE, text=" Item LC1 SS1 F1 LC2 SS2 F2 LC3 SS3 F3 A123 MW1 20 1 SW1 10 2 RM1 10 2 A123 MW1 20 1 WK1 5 4 NA NA NA B123 MW1 15 2 RS1 10 1 RM1 10 2 B123 MW1 15 2 RM1 10 2 RT1 5 1") x$SSN1 <- ifelse(x$F1 == 1L, 0, x$SS1) x$SSN2 <- ifelse(x$F1 == 1L, 0, ifelse(x$F2 == 1L, 0, x$SS2)) x$SSN3 <- ifelse(x$F1 == 1L, 0, ifelse(x$F2 == 1L, 0, ifelse(x$F3 == 1L, 0, x$SS3))) x # Item LC1 SS1 F1 LC2 SS2 F2 LC3 SS3 F3 SSN1 SSN2 SSN3 # 1 A123 MW1 20 1 SW1 10 2 RM1 10 2 0 0 0 # 2 A123 MW1 20 1 WK1 5 4 <NA> NA NA 0 0 0 # 3 B123 MW1 15 2 RS1 10 1 RM1 10 2 15 0 0 # 4 B123 MW1 15 2 RM1 10 2 RT1 5 1 15 10 0 实现:

data.table

(必须有一种更好的方法...我可以使它更容易使用library(data.table) xDT <- copy(x) setDT(xDT) xDT[F1 == 1L, c("SSN1", "SSN2", "SSN3") := 0L ] xDT[(is.na(SSN1)), SSN1 := SS1 ] xDT[(F2 == 1L & is.na(SSN2)), SSN2 := 0L] xDT[(is.na(SSN2)), SSN2 := SS2] xDT[((F2 == 1L | F3 == 1L) & is.na(SSN3)), SSN3 := 0L] xDT[(is.na(SSN3)), SSN3 := SS3] xDT # Item LC1 SS1 F1 LC2 SS2 F2 LC3 SS3 F3 SSN1 SSN2 SSN3 # 1: A123 MW1 20 1 SW1 10 2 RM1 10 2 0 0 0 # 2: A123 MW1 20 1 WK1 5 4 <NA> NA NA 0 0 0 # 3: B123 MW1 15 2 RS1 10 1 RM1 10 2 15 0 0 # 4: B123 MW1 15 2 RM1 10 2 RT1 5 1 15 10 0 进行阅读,但要使其看起来更加……)