简体   繁体   English

根据另一列中的值减去 R 中列中的值

[英]subtracting values a in column in R based on values in another column

I have data that looks like the following我有如下所示的数据

#    View  date    value1 Value2 
#     a  2012-10-01 21.01  2.00
#     b  2012-10-01 22.04  3.03
#     c  2012-10-01 22.65  7.61
#     a  2012-11-01 23.11  8.46
#     b  2012-11-01 35.21  9.00
#     c  2012-11-01 35.21  9.00

structure(list(View = c("a", "b", "c", "a", "b", "c"), date = c("2012-10-01", 
"2012-10-01", "2012-10-01", "2012-11-01", "2012-11-01", "2012-11-01"
), value1 = c(21.01, 22.04, 22.65, 23.11, 35.21, 35.21), Value2 = c(2, 
3.03, 7.61, 8.46, 9, 9)), .Names = c("View", "date", "value1", 
"Value2"), row.names = c(NA, -6L), class = "data.frame")

I want to create a new View "D" which is the subtraction of "a" from "c" for any given date.我想创建一个新的视图“D”,它是任何给定日期的“c”减去“a”。 ie end up with a dataset that looks like this?即最终得到一个看起来像这样的数据集?

#    View  date    value1 Value2 
#     a 2012-10-01 21.01  2.00
#     b 2012-10-01 22.04  3.03
#     c 2012-10-01 22.65  7.61
#     D 2012-10-01  1.61  5.61
#     a 2012-11-01 23.11  8.46
#     b 2012-11-01 35.21  9.00
#     c 2012-11-01 35.21  9.00
#     D 2012-10-01 12.1   0.54

I know a bit about R but I have no idea how I can approach this.我对 R 有所了解,但我不知道如何解决这个问题。 Any suggestions would be greatly appreciated.任何建议将不胜感激。

You can rbind a new calculated row with .SD (sub data.table which comes from a unique date) after grouping your data.table by date :您可以rbind一个新的计算一行.SD通过分组的data.table后(它来自一个独特的日子data.table) date

df[, rbind(.SD, 
     .(View = "D", value1 = value1[View == "c"] - value1[View == "a"], 
                   Value2 = Value2[View == "c"] - Value2[View == "a"])), date]

#         date View value1 Value2
#1: 2012-10-01    a  21.01   2.00
#2: 2012-10-01    b  22.04   3.03
#3: 2012-10-01    c  22.65   7.61
#4: 2012-10-01    D   1.64   5.61
#5: 2012-11-01    a  23.11   8.46
#6: 2012-11-01    b  35.21   9.00
#7: 2012-11-01    c  35.21   9.00
#8: 2012-11-01    D  12.10   0.54

To avoid hard coding column names, but still assume you have date and View columns to manipulate:为了避免对列名进行硬编码,但仍假设您有dateView列进行操作:

# drop View column so that you can do subtraction
df[, rbind(.SD, { dt = .SD[, !"View", with = F];      
                 # subtract row c and row a and assign a new View column as D           
                 (dt[View == "c"] - dt[View == "a"])[, View := "D"][] }), date]

#         date View value1 Value2
#1: 2012-10-01    a  21.01   2.00
#2: 2012-10-01    b  22.04   3.03
#3: 2012-10-01    c  22.65   7.61
#4: 2012-10-01    D   1.64   5.61
#5: 2012-11-01    a  23.11   8.46
#6: 2012-11-01    b  35.21   9.00
#7: 2012-11-01    c  35.21   9.00
#8: 2012-11-01    D  12.10   0.54

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM