Таблица трансформации с помощью dplyr - PullRequest
0 голосов
/ 08 мая 2019

У меня есть таблица с именем df. Эта таблица содержит три столбца (код, описание и скорость).

enter image description here

#CODE

df<-data.frame(
  Code=c("01","0101","0101 21 00 00","0101 29","0101 29 10 00","0101 29 
          90 00","0101 30 00 00","0101 90 00 00","NA","0102 21","0102 21 10 
          00","0102 21 30 00","0102 21 90 00"),
  Description=c("LIVE ANIMALS", "Live horses, asses, mules","Live horses, 
                asses, mules and hinnies","Pure-bred breeding horses","Live 
                horses (excl. pure-bred for breeding)","Horses for 
                slaughter","Live horses (excl. for slaughter, pure-bred for 
                breeding)","Live asses","Live mules and hinnies","Live 
                bovine animals","Pure-bred cattle for breeding","Pure-bred 
                breeding heifers female bovines that have never 
                calved","Pure-bred breeding cows (excl. heifers)"),
  Rate=c("NA","NA","5","NA","5","10","15","7","NA","NA","10","15","20"))

Таким образом, я собираюсь создать подмножество верхней таблицы, которое будет содержать только поле столбца Code, которое имеет 10 цифр и вычисляет среднее значение. Это означает, что код должен извлекать только строки с 10-значными кодами (0101 21 00 00,0101 29 10 00, 0101 29 90 00,0101 30 00 00,0101 90 00 00,0102 21 10 00,0102 21 30 00 и 0102 21 90 00), как в таблице ниже. Среднее значение столбца Rate составляет 2,75.

enter image description here

Так кто-нибудь может мне помочь, как преобразовать эту таблицу?

1 Ответ

1 голос
/ 08 мая 2019

Мы можем удалить пробелы из кодов и затем посчитать символы. Это позволяет использовать от filter до 10-значных кодов. Затем мы можем добавить summarise, если нам нужно среднее значение (обратите внимание, что это не 2,75)

library(tidyverse)
df <- tibble(Code = c("01", "0101", "0101 21 00 00", "0101 29", "0101 29 10 00", "0101 29 90 00", "0101 30 00 00", "0101 90 00 00", "NA", "0102 21", "0102 21 10 00", "0102 21 30 00", "0102 21 90 00"), Description = c("LIVE ANIMALS", "Live horses, asses, mules", "Live horses,  asses, mules and hinnies", "Pure-bred breeding horses", "Live horses (excl. pure-bred for breeding)", "Horses for slaughter", "Live horses (excl. for slaughter, pure-bred for breeding)", "Live asses", "Live mules and hinnies", "Live  bovine animals", "Pure-bred cattle for breeding", "Pure-bred breeding heifers female bovines that have never calved", "Pure-bred breeding cows (excl. heifers)"), Rate = c("NA", "NA", "5", "NA", "5", "10", "15", "7", "NA", "NA", "10", "15", "20"))
df %>%
  filter(Code %>% str_remove_all("\\s") %>% str_length %>% `==`(10))
#> # A tibble: 8 x 3
#>   Code         Description                                            Rate 
#>   <chr>        <chr>                                                  <chr>
#> 1 0101 21 00 … Live horses,  asses, mules and hinnies                 5    
#> 2 0101 29 10 … Live horses (excl. pure-bred for breeding)             5    
#> 3 0101 29 90 … Horses for slaughter                                   10   
#> 4 0101 30 00 … Live horses (excl. for slaughter, pure-bred for breed… 15   
#> 5 0101 90 00 … Live asses                                             7    
#> 6 0102 21 10 … Pure-bred cattle for breeding                          10   
#> 7 0102 21 30 … Pure-bred breeding heifers female bovines that have n… 15   
#> 8 0102 21 90 … Pure-bred breeding cows (excl. heifers)                20

df %>%
  filter(Code %>% str_remove_all("\\s") %>% str_length %>% `==`(10)) %>%
  summarise(mean_rate = mean(as.integer(Rate)))
#> # A tibble: 1 x 1
#>   mean_rate
#>       <dbl>
#> 1      10.9

Создано в 2019-05-08 пакетом Представления (v0.2.1)

...