0

There are many other questions which read similar to this, but none address my query [in a way I understand]

I have multiple dataframes: 'snps' 'snp2' 'snp3' 'snp4' 'snp5' which all have the same format

library(tidyr)
library(TwoSampleMR)
glimpse(snps)
Rows: 4,873
Columns: 4
$ chrpos       <chr> "6:39016574-39055519", "6:39016574-39055519", "6:39016574-39055519"
$ target       <chr> "GL", "GL", "GL"
$ query        <chr> "6:39016574-39055519", "6:39016574-39055519", "6:39016574-39055519"
$ name         <chr> "rs113920163", "rs183723208", "rs555466268"

The only thing that differs is the 'target' column. I want to run a function from the TwoSampleMR package

glc <- extract_outcome_data(snps = snps$name, outcomes = 'ukb-a-583')

But I want to do this for the other dataframes, in a loop

I made a list of all the names in each of the 'snps..' dataframes called targets

glimpse(targets)
Rows: 11
Columns: 1
$ target <chr> "GL", "ML", "HL", "TD", "ED"

And have been trying to loop through

list <- targets$target
for(i in list)
  l <- list()
{
  [[i]] <- extract_outcome_data(snps = targets$target, outcomes = 'ukb-a-583')
}

Which runs the but the object it makes 'l' is empty but there is no error msg so I don't know what to change.

Thanks!

*** EDIT ***

I am now trying

my_files <- list.files(pattern = "*_files.txt")

my_data <- lapply(my_files, read.table)

for (i in 1:length(my_data)){
  dat <- extract_outcome_data(snps = my_data$[i]$targets, outcomes='ukb-a-583'))))
}

It works fine until the for loop and the for loop does run. However this does not extract the information that I need. I think its due to the 'snps = my_data$[i]$targets' bit - how do I access the column 'targets' in each of my dataframes in the my_data list?

Thanks!

ayeepi
  • 135
  • 8
  • 2
    I don't know what you intend to do, e.g., `[[i]] <- extract...`, my guess is you should be including the name of the list of frames there. Regardless, it might be informative to read about [lists of frames](https://stackoverflow.com/a/24376207/3358227), where it walks through rationale to having like-frames kept in a `list(..)`, and how to efficiently operate on that list of frames. – r2evans Nov 02 '22 at 13:52
  • If this is the actual code, I'm afraid your loop ends *before* curly braces. – margusl Nov 02 '22 at 14:14
  • What do you mean sorry @margusl? I have edited the question to remove the erroneous 'exposure[i]' command, is that what you meant? – ayeepi Nov 02 '22 at 15:11
  • Not quite, construct for for-loop is `for(var in seq) expr`, currently only expression in that loop is `l <- list()` , braces should start right after closing parenthesis of the for: `for(var in seq) {expr1; expr2}` – margusl Nov 02 '22 at 15:29
  • @r2evans thank you I have edited my question accordingly. Do you know what I am missing? Thanks! – ayeepi Nov 02 '22 at 15:40
  • 1
    To access columns of dataframes in a list you can use `my_data[[list_index]]$column`. Though you really should not need that loop anymore as you can apply your function to each dataframe in the list: `list_of_results <- lapply(my_data, \(x) extract_outcome_data(snps = x$target, outcomes='ukb-a-583'))` – margusl Nov 02 '22 at 17:22

0 Answers0