Добавить столбец данных в существующий столбец данных в R? - PullRequest
1 голос
/ 20 марта 2020

У меня есть блок данных компаний и отдельный блок данных с категориями, по которым я хочу оценить каждую из этих компаний; то, что я хочу сделать, это добавить мои данные категории данных в виде столбца данных данных компании, а затем unnest() этот столбец. В конце я хочу создать файл excel / csv, который моя команда может использовать для отслеживания оценки компаний.

Вот мои данные:

companies <- tibble(company = c("company_a", "company_b", "company_c"))

# and here's the structure for my assessment criteria

structure(list(id = c(NA_character_, NA_character_, NA_character_, 
NA_character_, NA_character_, NA_character_, NA_character_, NA_character_, 
NA_character_, NA_character_, NA_character_, NA_character_, NA_character_, 
NA_character_, NA_character_, NA_character_, NA_character_, NA_character_, 
NA_character_, NA_character_), evaluator = c(NA_character_, NA_character_, 
NA_character_, NA_character_, NA_character_, NA_character_, NA_character_, 
NA_character_, NA_character_, NA_character_, NA_character_, NA_character_, 
NA_character_, NA_character_, NA_character_, NA_character_, NA_character_, 
NA_character_, NA_character_, NA_character_), subawardee_name = c(NA_character_, 
NA_character_, NA_character_, NA_character_, NA_character_, NA_character_, 
NA_character_, NA_character_, NA_character_, NA_character_, NA_character_, 
NA_character_, NA_character_, NA_character_, NA_character_, NA_character_, 
NA_character_, NA_character_, NA_character_, NA_character_), 
    category = c("category_1", "category_1", "category_1", "category_1", 
    "category_1", "category_2", "category_2", "category_2", "category_2", 
    "category_2", "category_3", "category_3", "category_3", "category_3", 
    "category_3", "category_4", "category_4", "category_4", "category_4", 
    "category_4"), level = c("Ownership", "Leadership", "Employees", 
    "Operations", "Product", "Ownership", "Leadership", "Employees", 
    "Operations", "Product", "Ownership", "Leadership", "Employees", 
    "Operations", "Product", "Ownership", "Leadership", "Employees", 
    "Operations", "Product"), rating = c(NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_), excerpt = c(NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_), source = c(NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_)), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -20L))

Поэтому я хочу присоединиться к этому датафрейм в виде отдельной строки для каждой компании. Я пробовал это, но это не сработало:

companies %>% 
    group_by(company) %>% 
    mutate(data = assessment_criteria)

Error: Column `data` is of unsupported class data.frame

Я также пробовал различные объединения с комбинациями map_df и map2_df. Есть мысли здесь?

1 Ответ

2 голосов
/ 20 марта 2020

Вместо этого вам просто нужно предоставить фреймы данных в виде списка.

library(dplyr)

companies_nest <- companies %>%
  mutate(data = list(other_data)) 

companies_nest
#> # A tibble: 3 x 2
#>   company   data             
#>   <chr>     <list>           
#> 1 company_a <tibble [20 x 8]>
#> 2 company_b <tibble [20 x 8]>
#> 3 company_c <tibble [20 x 8]>

Затем можно при необходимости удалить их.

library(tidyr)
companies_nest %>%
  unnest(cols = c(data))
#> # A tibble: 60 x 9
#>    company  id    evaluator subawardee_name category level rating excerpt source
#>    <chr>    <chr> <chr>     <chr>           <chr>    <chr> <chr>  <chr>   <chr> 
#>  1 company~ <NA>  <NA>      <NA>            categor~ Owne~ <NA>   <NA>    <NA>  
#>  2 company~ <NA>  <NA>      <NA>            categor~ Lead~ <NA>   <NA>    <NA>  
#>  3 company~ <NA>  <NA>      <NA>            categor~ Empl~ <NA>   <NA>    <NA>  
#>  4 company~ <NA>  <NA>      <NA>            categor~ Oper~ <NA>   <NA>    <NA>  
#>  5 company~ <NA>  <NA>      <NA>            categor~ Prod~ <NA>   <NA>    <NA>  
#>  6 company~ <NA>  <NA>      <NA>            categor~ Owne~ <NA>   <NA>    <NA>  
#>  7 company~ <NA>  <NA>      <NA>            categor~ Lead~ <NA>   <NA>    <NA>  
#>  8 company~ <NA>  <NA>      <NA>            categor~ Empl~ <NA>   <NA>    <NA>  
#>  9 company~ <NA>  <NA>      <NA>            categor~ Oper~ <NA>   <NA>    <NA>  
#> 10 company~ <NA>  <NA>      <NA>            categor~ Prod~ <NA>   <NA>    <NA>  
#> # ... with 50 more rows

Данные

companies <- tibble(company = c("company_a", "company_b", "company_c"))

other_data <- structure(list(id = c(NA_character_, NA_character_, NA_character_, 
                      NA_character_, NA_character_, NA_character_, NA_character_, NA_character_, 
                      NA_character_, NA_character_, NA_character_, NA_character_, NA_character_, 
                      NA_character_, NA_character_, NA_character_, NA_character_, NA_character_, 
                      NA_character_, NA_character_), evaluator = c(NA_character_, NA_character_, 
                                                                   NA_character_, NA_character_, NA_character_, NA_character_, NA_character_, 
                                                                   NA_character_, NA_character_, NA_character_, NA_character_, NA_character_, 
                                                                   NA_character_, NA_character_, NA_character_, NA_character_, NA_character_, 
                                                                   NA_character_, NA_character_, NA_character_), subawardee_name = c(NA_character_, 
                                                                                                                                     NA_character_, NA_character_, NA_character_, NA_character_, NA_character_, 
                                                                                                                                     NA_character_, NA_character_, NA_character_, NA_character_, NA_character_, 
                                                                                                                                     NA_character_, NA_character_, NA_character_, NA_character_, NA_character_, 
                                                                                                                                     NA_character_, NA_character_, NA_character_, NA_character_), 
               category = c("category_1", "category_1", "category_1", "category_1", 
                            "category_1", "category_2", "category_2", "category_2", "category_2", 
                            "category_2", "category_3", "category_3", "category_3", "category_3", 
                            "category_3", "category_4", "category_4", "category_4", "category_4", 
                            "category_4"), level = c("Ownership", "Leadership", "Employees", 
                                                     "Operations", "Product", "Ownership", "Leadership", "Employees", 
                                                     "Operations", "Product", "Ownership", "Leadership", "Employees", 
                                                     "Operations", "Product", "Ownership", "Leadership", "Employees", 
                                                     "Operations", "Product"), rating = c(NA_character_, NA_character_, 
                                                                                          NA_character_, NA_character_, NA_character_, NA_character_, 
                                                                                          NA_character_, NA_character_, NA_character_, NA_character_, 
                                                                                          NA_character_, NA_character_, NA_character_, NA_character_, 
                                                                                          NA_character_, NA_character_, NA_character_, NA_character_, 
                                                                                          NA_character_, NA_character_), excerpt = c(NA_character_, 
                                                                                                                                     NA_character_, NA_character_, NA_character_, NA_character_, 
                                                                                                                                     NA_character_, NA_character_, NA_character_, NA_character_, 
                                                                                                                                     NA_character_, NA_character_, NA_character_, NA_character_, 
                                                                                                                                     NA_character_, NA_character_, NA_character_, NA_character_, 
                                                                                                                                     NA_character_, NA_character_, NA_character_), source = c(NA_character_, 
                                                                                                                                                                                              NA_character_, NA_character_, NA_character_, NA_character_, 
                                                                                                                                                                                              NA_character_, NA_character_, NA_character_, NA_character_, 
                                                                                                                                                                                              NA_character_, NA_character_, NA_character_, NA_character_, 
                                                                                                                                                                                              NA_character_, NA_character_, NA_character_, NA_character_, 
                                                                                                                                                                                              NA_character_, NA_character_, NA_character_)), class = c("tbl_df", 
                                                                                                                                                                                                                                                       "tbl", "data.frame"), row.names = c(NA, -20L))
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...