String - Search, Count and Summary

Hello there,

Let's say I have the following data, how can I search the number of Users per country?

User<-c("B","A", "C", "D","D","A","C","A","D","D","B")
Location<-c("Australia","Japan","Italy","EUA","EUA","Australia","Italy","Japan",
"Australia","Japan","Japan")
Travel<-cbind(User,Location)
summary(Travel)
Travel[grep("D",Travel),]
Travel[grep("C",Travel),]

#Outputs:

summary(Travel)
User Location
A:3 Australia:3
B:2 EUA :2
C:2 Italy :2
D:4 Japan :4

Travel[grep("D",Travel),]
User Location
[1,] "D" "EUA"
[2,] "D" "EUA"
[3,] "D" "Australia"
[4,] "D" "Japan"

Travel[grep("C",Travel),]
User Location
[1,] "C" "Italy"
[2,] "C" "Italy"

As you can see, D was in 4 locations, twice in EUA, also C was twice in Italy, but I need to count only once. The result that I am looking for is something like this:

Country --- Count
Australia -- 3
EUA -------- 1
Italy -------- 1
Japan ------ 1

I am looking for a function (combination of fucntions) since I have too much data to analyse, but any help would be great.

I really appreciate any help.

Regards,
Luiz.

I'm not sure if I understand your question but I think this is what you are trying to do

library(dplyr)

travel <- data.frame(stringsAsFactors = FALSE,
                     user = c("B", "A", "C", "D", "D", "A", "C", "A", "D", "D", "B"),
                     location = c("Australia","Japan","Italy","EUA","EUA","Australia","Italy","Japan",
                                  "Australia","Japan","Japan"))

travel %>% 
    distinct() %>% 
    count(location)
#> # A tibble: 4 x 2
#>   location      n
#>   <chr>     <int>
#> 1 Australia     3
#> 2 EUA           1
#> 3 Italy         1
#> 4 Japan         3

Created on 2019-01-24 by the reprex package (v0.2.1)

2 Likes

That was exactly what I was looking for, Thank you so much!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.