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
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
CodePudding user response:
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
CodePudding user response:
If I'm understanding the goal, you can make a table of prices, then merge it in to temp_dhcp_count
as needed:
library(tidyverse)
prices <- tribble(
~ AddressState, ~ price,
"Active", 40,
"ActiveReservation", 100,
"Declined", 50,
"Expired", 50,
"InactiveReservation", 120
)
temp_dhcp_count %>%
left_join(prices) %>%
mutate(total = quantity * price)
# # A tibble: 5 x 4
# AddressState quantity price total
# <chr> <dbl> <dbl> <dbl>
# 1 Active 6323 40 252920
# 2 ActiveReservation 1222 100 122200
# 3 Declined 10 50 500
# 4 Expired 12 50 600
# 5 InactiveReservation 287 120 34440
This will work regardless of the order of AddressState
in temp_dhcp_count
.