0

I am working with log data from a website and I have one variable indicating the browser, e.g.,

Mozilla/5.0 (Macintosh; Intel Mac OS X 11_2_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36

for PC

or

Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1

for Mobile.

I know that for Python there is a library to do this, is there an equivalent solution for R?

I tried the uaparserjs package and it gives out things like device family, device brand etc, but how can I create a variable indicating the device in the categories PC, Mobile, or Tablet?

library(uaparserjs)
ua_parse("Mozilla/5.0 (Linux; Android 10; HRY-LX1T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.105 Mobile Safari/537.36")

# A tibble: 1 x 10
  userAgent                                       ua.family  ua.major ua.minor ua.patch os.family os.major device.family device.brand  device.model
  <chr>                                           <chr>      <chr>    <chr>    <chr>    <chr>     <chr>    <chr>         <chr>         <chr>       
1 Mozilla/5.0 (Linux; Android 10; HRY-LX1T) Appl~ Chrome Mo~ 89       0        4389     Android   10       HRY-LX1T      Generic_Andr~ HRY-LX1T   

Ideally, I would be able to detect PC vs. mobile vs. tablet.

Scijens
  • 541
  • 2
  • 11

1 Answers1

1

Differentiating PC from mobile/tablet is easy: just use the "os.family" column from ua_parse.

But to differentiate mobile and tablet precisely, you would need some screen info (physical size, resolution, density...) Detect phone vs tablet

crestor
  • 1,388
  • 8
  • 21
  • Thanks. Would you just use an ifelse statement and manually assign the os.family to the device? Something like device <- ifelse(os.family == "Anroid" | "iOS", "mobile", "desktop") ? I was wondering if there is no package to directly retrieve the device from the user agent / browser information. – Scijens Apr 14 '21 at 08:36
  • I would use a `dplyr::case_when`. But, yes, it's similar to doing it with many `if_else`. – crestor Apr 14 '21 at 11:13