简体   繁体   中英

How to replicate Excel's COUNTIF in R?

How to write below Excel code in R?

COUNTIF($A$4:A4,A4) 

I have > 100k rows of data where I want to fetch COUNTIF($A$4:A4,A4) = 1 value. I am able to do it in Excel, however, I am facing issue in R

Date            Worker ID

10/31/2017     3152
9/30/2017      3152
8/31/2017      3152
7/31/2017      3152
6/30/2017      3152
5/31/2017      3152
4/30/2017      3152
3/31/2017      3152
2/28/2017      3153
1/31/2017      3153
12/31/2016     3153
11/30/2016     3153
10/31/2017     3153
9/30/2017      3153
8/31/2017      3153
7/31/2017      3153
6/30/2017      3153
5/31/2017      3940
4/30/2017      3940
3/31/2017      3940
2/28/2017      3940
1/31/2017      3940

I have the same set of data with 25 columns where every rows have different data set but the latest/last date has updated information. I want to pick the latest date rows of the employee.

You can use data frame subsetting and duplicated function to imitate Excel's COUNTIF . Please see the code below:

df <- structure(list(Date = structure(c(2L, 12L, 11L, 10L, 9L, 8L, 
7L, 6L, 5L, 1L, 4L, 3L, 2L, 12L, 11L, 10L, 9L, 8L, 7L, 6L, 5L, 
1L), .Label = c("1/31/2017", "10/31/2017", "11/30/2016", "12/31/2016", 
"2/28/2017", "3/31/2017", "4/30/2017", "5/31/2017", "6/30/2017", 
"7/31/2017", "8/31/2017", "9/30/2017"), class = "factor"), Worker_ID = c(3152L, 
3152L, 3152L, 3152L, 3152L, 3152L, 3152L, 3152L, 3153L, 3153L, 
3153L, 3153L, 3153L, 3153L, 3153L, 3153L, 3153L, 3940L, 3940L, 
3940L, 3940L, 3940L)), class = "data.frame", row.names = c(NA, 
-22L))


df[!duplicated(df$Worker_ID), ]

Output:

         Date Worker_ID
1  10/31/2017      3152
9   2/28/2017      3153
18  5/31/2017      3940

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