简体   繁体   中英

R: Select Rows from Data Frame based on condition

I have the following data frames

User_Details:

+-------------+-----------+-----------+
|   Name      |  Address  |   Phone   |
+-------------+-----------+-----------+
| John Doe    | Somewhere | 123456789 |
| Jane Doe    | Somewhere | 234567891 |
| Jack Russel | Somewhere | 234567891 |
+-------------+-----------+-----------+

User_Transaction_Count:

+-------------+-----------+
|   Name      | Frequency |
+-------------+-----------+
| John Doe    | 2         |
| Jane Doe    | 5         |
| Jack Russel | 2         |
+-------------+-----------+

What I want to do is get the details of the user with the most transactions. So in the above case, Jane Doe has the most transactions, so I need to fetch her details into a data frame.

I tried the following code:

User_details[which(user_details$Name = User_Transaction_Count[(which.max(User_Transaction_Count$Frequency)),]$Name)]

But I get this error:

Error: unexpected '=' in "ad_maxState <- accidental_deaths[which(accidental_deaths$State ="

我对T.Ciffréo的答案进行了一些更改,并找到了解决方案:

User_details[User_details$Name==as.character(User_transaction_Count[which.max(User_transaction_Count$Frequency),]$Name),]

To determine the user with the maximum Frequency, we can use:

with(User_Transaction_Count,Name[[which.max(Frequency)]])

However, if the User column is using the factor() datatype (which is usually the default), we need to convert it to a string to be used for lookup. Otherwise internal value for "John Doe" in one data.frame may not be the same as "John Doe" in the other.

maxUser <- as.character(with(User_Transaction_Count,Name[[which.max(Frequency)]]))

Then we can perform the lookup in the other data.frame .

result <- User_Details[User_Details$Name == maxUser,]

This may take a long time if the table is very large, so it may be best to create an index for this

#build index
library(hash)
userIdx <- hash(as.character(User_Details$Name),1:nrow(User_Details))

#use index
maxUser <- as.character(with(User_Transaction_Count,Name[[which.max(Frequency)]]))
result <- User_Details[userIdx[[maxUser]],]

Output:

> result
      Name   Address     Phone
2 Jane Doe Somewhere 234567891

码:

User_details[User_details$Name==User_transaction_Count[max(User_transaction_Count$Frequency),]$Name,]$Name

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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