Home > OS >  How do you change the output of count from a tibble to Name Value pairs?
How do you change the output of count from a tibble to Name Value pairs?

Time:03-03

I need to use the values generated by count() for use in formulas.

temp_dhcp %>% 
  group_by(AddressState) %>% 
  count(name = "quantity") -> temp_dhcp_count

temp_dhcp %>% 
  count(AddressState, name = "quantity")

Generates a tibble:

# A tibble: 5 x 2
  AddressState        quantity
  <chr>                  <int>
1 Active                  6323
2 ActiveReservation       1222
3 Declined                  10
4 Expired                   12
5 InactiveReservation      287

I need to take the quantity per line and apply different calculations. Is there a way to convert each row to the equivalent of

Active = 6323
ActiveReservation = 1222
Declined = 10
Expired = 12
InactiveReservation = 287

Thank you,

-Jacob


Sample output of temp_dhcp as requested by @neilfws

> temp_dhcp
# A tibble: 697 x 3
   IPAddress   AddressState        HostName               
   <chr>       <chr>               <chr>                  
 1 10.75.2.69  InactiveReservation REDACTED
 2 10.75.2.72  InactiveReservation REDACTED      
 3 10.75.2.79  InactiveReservation REDACTED
 4 10.75.2.91  InactiveReservation REDACTED
 5 10.75.2.93  InactiveReservation REDACTED               
 6 10.75.2.94  InactiveReservation REDACTED               
 7 10.75.2.95  InactiveReservation REDACTED               
 8 10.75.2.96  InactiveReservation REDACTED              
 9 10.75.2.101 InactiveReservation REDACTED               
10 10.75.2.102 InactiveReservation REDACTED               
# ... with 687 more rows

CodePudding user response:

glue::glue_data can be used for this:

quux %>%
  glue::glue_data("{AddressState} = {quantity}")
# Active = 6323
# ActiveReservation = 1222
# Declined = 10
# Expired = 12
# InactiveReservation = 287

Data

quux <- structure(list(AddressState = c("Active", "ActiveReservation", "Declined", "Expired", "InactiveReservation"), quantity = c(6323L, 1222L, 10L, 12L, 287L)), class = "data.frame", row.names = c("1", "2", "3", "4", "5"))

CodePudding user response:

You can change your tibble to a named vector like this:

temp_dhcp_count_vec <- temp_dhcp_count$quantity
names(temp_dhcp_count_vec) <- temp_dhcp_count$AddressState

temp_dhcp_count_vec
#>          dataActive   ActiveReservation            Declined             Expired 
#>                6323                1222                  10                  12 
#> InactiveReservation 
#>                 287

or like this:

library(magrittr)
temp_dhcp_count_vec <- temp_dhcp_count %$%
  set_names(quantity, AddressState)

temp_dhcp_count_vec
#>          dataActive   ActiveReservation            Declined             Expired 
#>                6323                1222                  10                  12 
#> InactiveReservation 
#>                 287

...but, as discussed in the comments to your question, I'm not sure if this is actually the best approach to whatever you're trying to do.

CodePudding user response:

vals <- with(temp_dhcp_count, setNames(as.list(quantity), AddressState)) Gets me exactly what I need.

Thanks MrFlick

  • Related