我有一个包含压力数据的大型数据集。我希望能够创建多组以各种值过滤的数据(即
例如,这可能与我目前所做的类似:
#making a reproducible example
pressure <- runif(30, min = 3750, max = 4500)
value <- runif(30, min = 0, max = 50)
stage <- rep(c(1, 2), each = 15)
raw.data <- data.frame(pressure, value, stage)
#set a cutoff point
cutoff.press <- 3750
#make a new dataset
cutoff <- raw.data[raw.data$pressure > cutoff.press,]
#run an analysis
analysis <- cutoff %>%
group_by(stage) %>%
summarize(
MinValue = min(value),
MaxValue = max(value)
)
有没有一种方法可以做到这一点,而不必为每个感兴趣的截止值创建多个单独的数据集,然后单独运行每个分析?
就像如果我想测试多个压力截止值,例如seq(3750,4000,50)
,我不想对序列中生成的每个值重复上述过程。
我考虑过将< code>dplyr与< code>filter()函数一起使用,并手动设置一组值,但这不仅会很耗时,而且我不确定这是否允许我对多个数据集进行分析。
这是一个相当广泛的问题,所以任何建议都会很棒。谢谢!
尝试< code>sapply函数,如下所示:
cutoff <- seq(3750, 4000, 50)
sapply(cutoff, function(y) raw.data %>%
filter(pressure > y) %>%
group_by(stage) %>%
summarize(
MinValue = min(value),
MaxValue = max(value)), simplify = FALSE)
>
我们可以通过case_when
语句创建分组或截止组。
分成这些组(您将获得一个列表)
将
映射到列表上,这也是Andrew Gillath Brown首次提供的!
library(purrr)
library(dplyr)
raw.data %>%
mutate(cut_off = case_when(pressure <= 4000 ~ "<= 4000",
TRUE ~ "> 4000")) %>%
group_split(cut_off) %>%
map(. %>%
group_by(stage) %>%
summarise(MinValue = min(value),
MaxValue = max(value)))
[[1]]
# A tibble: 2 x 3
stage MinValue MaxValue
<dbl> <dbl> <dbl>
1 1 4.72 48.7
2 2 33.9 47.3
[[2]]
# A tibble: 2 x 3
stage MinValue MaxValue
<dbl> <dbl> <dbl>
1 1 9.64 41.5
2 2 14.5 45.5
如果您有很多不同的迭代要运行,那么使用purrr
也是一个不错的选择,因为您可以在一个管道中完成所有操作。
library(tidyverse)
purrr::pmap(data.frame(pressure = seq(3750, 4000, 50)),
~ dplyr::filter(raw.data, pressure > ..1)) %>%
purrr::map(. %>%
group_by(stage) %>%
summarize(MinValue = min(value),
MaxValue = max(value))) %>%
# If you want to set the names to the cutoff values.
setNames(seq(3750, 4000, 50))
输出
$`3750`
# A tibble: 2 × 3
stage MinValue MaxValue
<dbl> <dbl> <dbl>
1 1 3.52 46.6
2 2 0.575 49.3
$`3800`
# A tibble: 2 × 3
stage MinValue MaxValue
<dbl> <dbl> <dbl>
1 1 3.52 46.6
2 2 0.575 47.5
$`3850`
# A tibble: 2 × 3
stage MinValue MaxValue
<dbl> <dbl> <dbl>
1 1 3.52 46.6
2 2 0.575 47.5
$`3900`
# A tibble: 2 × 3
stage MinValue MaxValue
<dbl> <dbl> <dbl>
1 1 3.52 46.6
2 2 0.575 47.5
$`3950`
# A tibble: 2 × 3
stage MinValue MaxValue
<dbl> <dbl> <dbl>
1 1 3.52 46.6
2 2 0.575 47.5
$`4000`
# A tibble: 2 × 3
stage MinValue MaxValue
<dbl> <dbl> <dbl>
1 1 6.65 46.6
2 2 0.575 47.5
raw.data <- structure(list(pressure = c(4160.41269886773, 4044.58961030468,
4336.48418885423, 3762.11064029485, 4235.55055609904, 3926.50744639104,
4086.0048676841, 4360.64667999744, 3850.74476944283, 3950.07681293646,
4347.61320002144, 3996.32209626725, 4262.53829378402, 3869.30528597441,
4252.7681372012, 4013.94325762521, 4275.64664371312, 4197.37908616662,
4231.71574808657, 4028.1643497292, 4407.9091984313, 4481.91399103962,
4353.40271308087, 4013.09538848, 4109.39885408152, 4195.05179609405,
4222.33691916335, 4316.15335500101, 3860.02388742054, 3772.72424055263
), value = c(46.6360261081718, 19.0778955002315, 9.46381011744961,
17.4791521392763, 6.64818733930588, 3.79822270479053, 17.0007253182121,
45.9705576649867, 39.6164933103137, 3.52405618177727, 29.9587145447731,
10.8624027809128, 45.8421137067489, 34.4845326268114, 17.0537169324234,
47.0035993610509, 29.5542735257186, 12.992845242843, 32.0275551988743,
21.112488291692, 12.7272683312185, 23.9938693121076, 18.5264392290264,
42.9235454765148, 0.575024168938398, 10.7687710318714, 0.992469629272819,
47.4592371145263, 40.4172958689742, 49.3020136258565),
stage = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2)), class = "data.frame", row.names = c(NA, -30L))
问题内容: 假设我有一个像这样的数据库列“ grade”: SQL中是否有非平凡的方式来生成像这样的直方图? 其中2表示1级出现两次,1表示{2..5}级出现一次,0表示6级完全没有出现。 我不介意直方图是否每计数一行。 如果那很重要,则该数据库是由perl CGI通过unixODBC / FreeTDS访问的SQL Server。 编辑: 感谢您的快速答复!只要我能确定哪个直方图值属于哪个等级,
问题内容: 我有一个名为Vendor的表,在此表中有一个名为AccountTerms的列,该列仅显示一个值(即0、1、2、3),依此类推。我也有一个要使用()的列,以反映该值的含义,例如: 等等… 我需要的是一个脚本,它将查看AccountTerms中的值,然后将更新以显示上面显示的单词值。我该怎么做呢? 问题答案: 我将尝试以一种尽可能简单的方式来解释这一点,以便于理解: 假设您有一个这样的表设
我在Scala中的中转置值时遇到问题。我的初始如下所示: 和是类型
我有两个火花数据集,其中一个列的帐户和键,键列在数组的格式[key1, key2, key3...]和另一个数据集的两个列的帐户和键值是在json.帐户,{key:值,键,值...}。我需要更新第二个数据集中的值,如果键出现在第一个数据集中。 预期产出
创建一个Java方法,它将列表作为参数(MasterList)并返回另一个列表(ExpectedList)。 列表中每个对象都有两个变量: null 我试图实现的逻辑是:当有多个ID相同的对象时,只考虑计数较大的特定对象。表示ID:有3个对象,所以我只考虑(ID:abc122,Count:20),因为在ID:abc122的对象中Count更高。在结束时,方法应返回
我有点被困在提取一个变量的值和另一个变量的条件上。例如,以下数据帧: 当时,如何获取的值?每次提取的值时,都会得到一个对象,而不是字符串。