由于源数据的动态性质,我需要根据名称而不是位置使用count()中的值。
我试图根据现有的ip分配状态估算重新ip设备的劳动力成本。
例:状态为活动状态的设备为$40.00,状态为ActiveReservation的设备为$100.00。
下面的第一个例子是: 6,323 *10美元,第二个例子是:9*10美元
我可以带他们过去
temp_dhcp_count$quantity[1] * 10但是我不能保证1是这个位置,并且总是“主动”的,我需要能够叫它的名字"Active“。
我的假设是,如果我能把它们提取成价值,我就可以:
> Active = 6323
> Active * 10
[1] 63230vs
temp_dhcp_count$quantity[1] * 10例如:
> 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和
> 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我试着问如何从Ti球中提取行作为键值对,现在我试图根据反馈来问这个问题。How do you change the output of count from a tibble to Name Value pairs?
源数据是我根据子网导入和选择的tsv,并按状态计数。
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经过更多的挖掘,
deframe() %>% as.list()也很管用。
发布于 2022-03-02 23:14:42
您可以创建一个命名列表。用样本数据
temp_dhcp_count <- read.table(text="
AddressState quantity
Active 6323
ActiveReservation 1222
Declined 10
Expired 12
InactiveReservation 287", header=TRUE)您可以创建一个命名的值列表,以按名称提取它们。
vals <- with(temp_dhcp_count, setNames(as.list(quantity), AddressState))
vals$Active
# [1] 6323
vals$Declined
# [1] 10如果vals$部件困扰您,您可以再次使用with()
with(vals, {
Active * 10 - Declined * 2
})
# [1] 63210发布于 2022-03-02 23:21:27
如果我理解目标,您可以制作一个价目表,然后根据需要将其合并到temp_dhcp_count中:
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不管AddressState在temp_dhcp_count中的顺序如何,这都是可行的。
https://stackoverflow.com/questions/71330222
复制相似问题