简体   繁体   English

如何根据R中的2列选择数据框的一行

[英]How to select a row of a dataframe depending of 2 columns in R

I currently have the following problem.我目前有以下问题。

I have a data frame of 150.000 rows and 4 columns, I need to filter by Profile ID (per day) and keep the last transaction that the user made (Profile ID), here is an example:我有一个 150.000 行和 4 列的数据框,我需要按配置文件 ID(每天)过滤并保留用户进行的最后一次交易(配置文件 ID),这是一个示例:

在此处输入图像描述

In the following image, the user ID "aToPCmXnXnOCgoGqEOVxY5pEnNM0h1" made two transactions, however, the last one he made (based on the Date column) is row 3656.在下图中,用户 ID“aToPCmXnXnOCgoGqEOVxY5pEnNM0h1”进行了两次交易,但是,他进行的最后一次交易(基于 Date 列)是第 3656 行。

What is the best way to select that row?选择该行的最佳方法是什么?

Initially, I thought about performing a double loop with an If inside, but it is very inefficient and time-consuming to perform this task.最初,我想过执行一个内部带有 If 的双循环,但执行此任务非常低效且耗时。

Try this尝试这个

library(dplyr)

df |> group_by(Profile.ID) |> summarise(Purchase = tail(Purchase , 1))
|> left_join(df)

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

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