Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I need help accessing a value in a tibble by name

I need to use the value from count() by name and not by position due to the dynamic nature of the source data.

I am trying to estimate the labor cost to re-ip devices based on existing ip assignment state.

Example:
a device with a state of Active will be $40.00
a device with a state of ActiveReservation will be $100.00

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

For the first example below:
6,323 * $10
For the second example below:
9 * $10

I can get them by

temp_dhcp_count$quantity[1] * 10

however I cant guarantee that [1] is the position and always "Active", I need to be able to call it by name "Active"

My assumption was, if I could extract them to values I could:

> Active = 6323
> Active * 10
[1] 63230

vs

temp_dhcp_count$quantity[1] * 10

For example:

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

> temp_dhcp_count$quantity[1]
[1] 6323

and

> temp_dhcp_count
# A tibble: 3 x 2
# Groups:   AddressState [3]
  AddressState        quantity
  <chr>                  <int>
1 Active                     9
2 ActiveReservation         46
3 InactiveReservation      642

> temp_dhcp_count$quantity[1]
[1] 9

I tried asking how to extract rows from a tibble as key value pairs and now I am trying to ask this way based on feedback.
How do you change the output of count from a tibble to Name Value pairs?

The source data is a tsv that I import and select based on subnet and count by state.

library(tidyverse)
library(ipaddress)
dhcp <- read_delim("dhcpmerge.tsv.txt", 
                   delim = "\t", escape_double = FALSE, 
                   trim_ws = TRUE)

dhcp <- distinct(dhcp)

network_in_review = "10.75.0.0/16"

temp_dhcp <- dhcp %>%
  select(IPAddress, AddressState, HostName) %>%
  filter(is_within(ip_address(IPAddress), ip_network(network_in_review)))

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

temp_dhcp_count

>Solution :

You can create a named list. With the sample data

temp_dhcp_count <- read.table(text="
AddressState        quantity
Active                  6323
ActiveReservation       1222
Declined                  10
Expired                   12
InactiveReservation      287", header=TRUE)

You can create a named list of values to extract them by name

vals <- with(temp_dhcp_count, setNames(as.list(quantity), AddressState))
vals$Active
# [1] 6323
vals$Declined
# [1] 10

And if the vals$ part bothers you, you can use with() again

with(vals, {
   Active * 10 - Declined * 2
})
# [1] 63210
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading