【发布时间】:2021-10-07 01:46:55
【问题描述】:
我正在尝试直接根据 tibble 中列出的信息制作对象,这些信息可以由我的环境中的后续函数/tibbles 调用。我可以手动制作对象,但我正在努力迭代。
library(tidyverse)
##determine mean from 2x OD Negatives in experimental plates, then save summary for use in appending table
ELISA_negatives = "my_file.csv"
neg_tibble <- as_tibble(read_csv(ELISA_negatives, col_names = TRUE)) %>%
group_by(Species_ab, Antibody, Protein) %>%
filter(str_detect(Animal_ID, "2x.*")) %>%
summarize(ave_neg_U_mL = mean(U_mL, na.rm = TRUE), n=sum(!is.na(U_mL)))
neg_tibble
# A tibble: 4 x 5
# Groups: Species_ab, Antibody [2]
Species_ab Antibody Protein ave_neg_U_mL n
<chr> <chr> <chr> <dbl> <int>
1 Mouse IgG GP 28.2 6
2 Mouse IgG NP 45.9 6
3 Rat IgG GP 5.24 4
4 Rat IgG NP 1.41 1
我可以根据上面的小标题手动编写对象:
Mouse_IgG_GP_cutoff <- as.numeric(neg_tibble[1,4])
Mouse_IgG_GP_cutoff
[1] 28.20336
在我尝试迭代地执行此操作时,我可以使用我需要的信息创建一个新的 tibble neg_tibble_string。我现在需要做的就是从第一列Test_Name 中的名称创建一个全局对象,并将其分配给第二列ave_neg_U_mL 中的数值(这是我卡住的地方)。
neg_tibble_string <- neg_tibble %>%
select(Species_ab:Protein) %>%
unite(col='Test_Name', c('Species_ab', 'Antibody', 'Protein'), sep = "_") %>%
mutate(Test_Name = str_c(Test_Name, "_cutoff")) %>%
bind_cols(neg_tibble[4])
neg_tibble_string
# A tibble: 4 x 2
Test_Name ave_neg_U_mL
<chr> <dbl>
1 Mouse_IgG_GP_cutoff 28.2
2 Mouse_IgG_NP_cutoff 45.9
3 Rat_IgG_GP_cutoff 5.24
4 Rat_IgG_NP_cutoff 1.41
我觉得必须有一种方法可以从上面的 tibble neg_tibble_string 中获取此信息,并为所有四行进行此操作。我尝试了this 和this 的变体,但无处可去。
> list_df <- mget(ls(pattern = "neg_tibble_string"))
> list_output <- map(list_df, ~neg_tibble_string$ave_neg_U_mL)
Warning message:
Unknown or uninitialised column: `ave_neg_U_mL`.
> list_output
$neg_tibble_string
NULL
一如既往,感谢任何见解!我的 R 之旅正在取得进展,但我知道我在知识方面缺少巨大的空白。
【问题讨论】:
-
您可以创建单独的对象,但您是否有理由不想保留数据框并根据需要提取特定值?
-
我将使用创建的对象过滤行,基于其他数据帧中的分组。例如,仅在值大于特定数据组(此数据框中的 Test_Name)的某个数字(此数据框中的数字)时保留行。
标签: r