How to extract data from a list with multiple groups?

kin182

I am new to R and I have a question about extracting data from a list with multiple groups. For example, I have a set of data like this:

data(iris)

iris$Group = rep(c("High","Low", each=5))
iris = iris[sample(nrow(iris)),]
mylist = list(iris[1:50,], iris[51:100,], iris[101:150,])

head(mylist)[[1]]

    Sepal.Length Sepal.Width Petal.Length Petal.Width    Species Group
51           7.0         3.2          4.7         1.4 versicolor  High
123          7.7         2.8          6.7         2.0  virginica  High
147          6.3         2.5          5.0         1.9  virginica   Low
23           4.6         3.6          1.0         0.2     setosa  High
120          6.0         2.2          5.0         1.5  virginica   Low
141          6.7         3.1          5.6         2.4  virginica  High

Within each list, I would like to group by Species and calculate the P value by t.test of Sepal.Length between Group High and Low. For example, I would like to get the P value of between Group High and Low of Species virginica, and so on for each list.

I am confused about this. Could anyone help? Thanks!

Maurits Evers

In base R you can do the following

lapply(mylist, function(x)
    with(x, t.test(Sepal.Length[Group == "High"], Sepal.Length[Group == "Low"])$p.value))
#[[1]]
#[1] 0.2751545
#
#[[2]]
#[1] 0.5480918
#
#[[3]]
#[1] 0.864256

Or a purrr/tidyverse approach

library(tidyverse)
bind_rows(mylist, .id = "id") %>%
    group_by(id) %>%
    nest() %>%
    mutate(pval = map_dbl(data, ~t.test(
        .x$Sepal.Length[.x$Group == "High"],
        .x$Sepal.Length[.x$Group == "Low"])$p.value))
## A tibble: 3 x 3
#  id    data               pval
#  <chr> <list>            <dbl>
#1 1     <tibble [50 × 6]> 0.275
#2 2     <tibble [50 × 6]> 0.548
#3 3     <tibble [50 × 6]> 0.864

Update

To perform t-tests of Sepal.Length between Group = "Low" and Group = "High" within Species you can do

lapply(mylist, function(x)
    with(x, setNames(sapply(unique(Species), function(y)
        t.test(Sepal.Length[Group == "High" & Species == y], Sepal.Length[Group == "Low" & Species == y])$p.value), unique(Species))))
#[[1]]
#versicolor  virginica     setosa
#0.80669755 0.07765262 0.47224383
#
#[[2]]
#    setosa  virginica versicolor
# 0.6620094  0.2859713  0.2427945
#
#[[3]]
#versicolor     setosa  virginica
# 0.5326379  0.6412661  0.5477179

Keep in mind that you will have to adjust raw p-values for multiple hypothesis testing.

To account for multiple hypothesis testing, you could modify above code slightly to give

lapply(mylist, function(x)
    with(x, p.adjust(setNames(sapply(unique(Species), function(y)
        t.test(Sepal.Length[Group == "High" & Species == y], Sepal.Length[Group == "Low" & Species == y])$p.value), unique(Species)))))
#[[1]]
#versicolor  virginica     setosa
# 0.9444877  0.2329579  0.9444877
#
#[[2]]
#    setosa  virginica versicolor
# 0.7283836  0.7283836  0.7283836
#
#[[3]]
#versicolor     setosa  virginica
#         1          1          1

Here we use p.adjust with the default Holm correction.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Extract multiple blocks from a List

How to extract data from a list of dictionaries from a filter

How to use grep to extract multiple groups

Extract list data from a column

How to extract data of the multiple keys from the Response. in ELM

How to extract into columns multiple components from a list within nested column

Extract data from heterogeneous list

How to extract specific data from json with multiple nodes?

How to extract data from the JSON file with multiple values

Extract from multiple regex named groups

How to extract Decoded data from a list?

PostgreSQL - How to extract multiple data from a column in postgresql

How can I extract data from multiple files in a folder of excel

how to extract from list python, element with index multiple of 2

How to extract data from multiple XML's into a table using AWK

How to extract consonant / vowels groups from a word ?

extract users from given list of groups and extract the data to every new group name in text using powershell

How to extract multiple data from JSON with a loop for create a CSV

Extract data from a list in python

How to extract data in one FOR loop from JSON multiple dictionary - Python

How to dynamically extract data from multiple Python dict entries

How to extract one of multiple matching values from list with priority?

how to extract data from an element list python

How to extract variable from a class with multiple variables, within a list

How to extract data from multiple SQL tables in one for multiple outputs

How to extract data from list in R with each list has multiple entries?

How to extract multiple strings from list spaced apart

How to plot the frequency of multiple groups of yearly data from a dataframe

How to extract data from list stored as a string like a list?