如何将两列的某些元素相乘?

时间:2019-07-05 14:40:30

标签: r dataframe

假设有3个不同的列:

    2019-07-05 14:32:54,242 ERROR [Timer-Driven Process Thread-1] o.a.nifi.processors.aws.s3.PutS3Object PutS3Object[id=c1e58db9-016b-1000-2b58-cc1f4bca459d] Failed to put StandardFlowFileRecord[uuid=78e10136-54b3-43c9-80ce-aa6941f9b369,claim=StandardContentClaim [resourceClaim=StandardResourceClaim[id=1562332079542-1, container=default, section=1], offset=132610, length=1],offset=0,name=65700097-8e40-4380-8324-12a589c865e8,size=1] to Amazon S3 due to com.amazonaws.services.securitytoken.model.AWSSecurityTokenServiceException: Access denied (Service: AWSSecurityTokenService; Status Code: 403; Error Code: AccessDenied; Request ID: c661e963-9f31-11e9-8f7e-450c08697371): com.amazonaws.services.securitytoken.model.AWSSecurityTokenServiceException: Access denied (Service: AWSSecurityTokenService; Status Code: 403; Error Code: AccessDenied; Request ID: c661e963-9f31-11e9-8f7e-450c08697371)
com.amazonaws.services.securitytoken.model.AWSSecurityTokenServiceException: Access denied (Service: AWSSecurityTokenService; Status Code: 403; Error Code: AccessDenied; Request ID: c661e963-9f31-11e9-8f7e-450c08697371)

当UNIT列为“每小时(5)”时,我想做

PAYPR         UNIT                 ACTDUR
40       (4) Per month               60 
100      (5) Per hour                30 
50       (6) Per year                10
5        (1) Per day                 20

所以输出是

 PAYPR<-PAYPR*ACTDUR/60 

2 个答案:

答案 0 :(得分:1)

创建逻辑索引并根据base R中的值修改“ PAYPR”的值

i1 <- df1$UNIT == "(5) Per hour"
df1$PAYPR[i1] <- df1$PAYPR[i1]* df1$ACTDUR[i1]/60
df1
#  PAYPR          UNIT ACTDUR
#1    40 (4) Per month     60
#2    50  (5) Per hour     30
#3    50  (6) Per year     10
#4     5   (1) Per day     20

数据

df1 <- structure(list(PAYPR = c(40L, 100L, 50L, 5L), UNIT = c("(4) Per month", 
"(5) Per hour", "(6) Per year", "(1) Per day"), ACTDUR = c(60L, 
30L, 10L, 20L)), class = "data.frame", row.names = c(NA, -4L))

答案 1 :(得分:1)

这可以在其他库中完成,但是我喜欢使用data.table,因为语法接近于基础data.frame,但是更简单。假设您的表存储在“ dt”中。然后:

library(data.table)
dt[UNIT == "(5) Per hour", PAYPR := PAYPR * ACTDUR / 60]

这称为切片。也可以用R来完成,但是我认为语法很草率:

dt[dt$UNIT == "(5)", "PAYPR"] <- dt[dt$UNIT == "(5)", "PAYPR"] * dt[dt$UNIT == "(5)", "ACTDUR"] / 30

我认为这最后一行可以综合一点,但是您会明白要点。