如何通过唯一ID将R中列中的某些行添加到一起?
我是新来的,如果我的问题措辞不好,我很抱歉 我在r工作,我有一张叫做Rent的桌子,可能看起来像这样:如何通过唯一ID将R中列中的某些行添加到一起?,r,uniqueidentifier,column-sum,R,Uniqueidentifier,Column Sum,我是新来的,如果我的问题措辞不好,我很抱歉 我在r工作,我有一张叫做Rent的桌子,可能看起来像这样: Rent ID Invoice Payment Paid Date lucy 7/1/2018 100 9/1/2018 lucy 7/1/2018 150 10/1/2018 lucy 8/1/2018 100 11/1/2018
Rent
ID Invoice Payment Paid Date
lucy 7/1/2018 100 9/1/2018
lucy 7/1/2018 150 10/1/2018
lucy 8/1/2018 100 11/1/2018
所以我想做的是,由于Lucy在2018年7月1日有两次付款,我想将它们合并在一起,然后将付款相加,并使用最新的付款日期
到目前为止我所知道的是
#to create a row that has the sum of the sales prices
Rent[,sum_late:=sum( as.numeric(("Sales Price"))),
by= c("Id","Invoice Date")]
#take the first of the unique IDs by the max paid date
head (SD,1) by=c("ID", "Invoice Date", max("Paid Date")
但是当我运行第一行时,所有sum_late列都是N/A。我不确定我做错了什么。理想情况下,我想要一张这样的桌子
Rent
ID Invoice Payment Paid Date
lucy 7/1/2018 250 10/1/2018
lucy 8/1/2018 100 11/1/2018
抱歉,如果这是一个愚蠢的问题,我感谢任何帮助和反馈!!谢谢大家抽出时间 我们可以将
付款日期
更改为日期类,按
ID
和发票
,金额
付款
选择max
付款日期
library(dplyr)
Rent %>%
mutate_at(vars(Invoice, Paid_Date), as.Date, '%d/%m/%Y') %>%
group_by(ID, Invoice) %>%
summarise(Payment = sum(Payment),
Paid_Date = max(Paid_Date))
# ID Invoice Payment Paid_Date
# <chr> <date> <int> <date>
#1 lucy 2018-01-07 250 2018-01-10
#2 lucy 2018-01-08 100 2018-01-11
数据
Rent <- structure(list(ID = c("lucy", "lucy", "lucy"), Invoice = c("7/1/2018",
"7/1/2018", "8/1/2018"), Payment = c(100L, 150L, 100L), Paid_Date = c("9/1/2018",
"10/1/2018", "11/1/2018")), class = "data.frame", row.names = c(NA, -3L))
Rent执行此任务有多种方法,我将使用for循环来创建所需的输出。我使用dplyr方法与@Ronak Shah进行回音,由于使用了循环,因此处理时间更短
资料
Rent你好Ronak,非常感谢!如果我的日期已经是2019-10-09格式,并且我实际上有除这三列以外的其他列,这是否意味着我只使用`图书馆(dplyr)租金%>%在(.%>%)组中按(ID,发票)%%>%汇总(付款=总额(付款),付款日期=最大(付款日期))`?您可以在group_by
中添加要保留的列。我没有运行变异代码,因为我的日期格式已经正确,但我发现即使我将其设置为max(paid date),后面的paid date行仍然存在。很抱歉问了一个简单的问题,我只编写了Rent%>%group_by('Id',Invoice'))%%>%摘要(Sales Price
=sum(Sales Price
),'Paid_Date'=max('Paid_Date'))1)不要直接复制粘贴代码,请使用数据中的列和数据框名称。通常,列的名称中最好没有空格,但如果列的名称中有空格,请将其与反引号一起使用。我喜欢这个“付款日期”
。2) 什么是类(租金$Paid\u日期)
?3) 在dplyr
/数据中指定列名时不要使用引号。表
函数,即“id”“Invoice”等,使用裸列名。
Rent <- structure(list(ID = c("lucy", "lucy", "lucy"), Invoice = c("7/1/2018",
"7/1/2018", "8/1/2018"), Payment = c(100L, 150L, 100L), Paid_Date = c("9/1/2018",
"10/1/2018", "11/1/2018")), class = "data.frame", row.names = c(NA, -3L))
Rent <- structure(list(ID = c("lucy", "lucy", "lucy"), Invoice = c("7/1/2018",
"7/1/2018", "8/1/2018"), Payment = c(100L, 150L, 100L), Paid_Date = c("9/1/2018",
"10/1/2018", "11/1/2018")), class = "data.frame", row.names = c(NA, -3L))
Rent$Paid_Date <- as.Date(Rent$Paid_Date, "%d/%m/%Y")
for ( i in unique (Rent$ID)){
for (j in unique(Rent$Invoice[Rent$ID == i])){
Rent$Payment_[Rent$ID==i & Rent$Invoice ==j ] <- sum (Rent$Payment [Rent$ID==i & Rent$Invoice ==j])
Rent$Paid_dt[Rent$ID==i & Rent$Invoice ==j ] <- max(Rent$Paid_Date[Rent$ID==i & Rent$Invoice ==j])
}
}
Rent$Paid_dt <- as.Date(Rent$Paid_dt ,origin = "1970-01-01") # converting into date format
Rent1 <- Rent[, unique(c("ID", "Invoice", "Payment_", "Paid_dt"))]
print (Rent1)
ID Invoice Payment_ Paid_dt
1 lucy 7/1/2018 250 2018-01-10
2 lucy 7/1/2018 250 2018-01-10
3 lucy 8/1/2018 100 2018-01-11