0

I'm trying to create a for loop in R to iterate through a list of genetic variants, labeled with rsID's, and filter the results by patient ID.

ace2_snps <- c("rs4646121", "rs4646127", "rs1996225", "rs2158082", "rs4830974", "rs148271868", "rs113539251", "rs4646135", "rs4646179", "rs2301693", "rs16980031", "rs12689012", "rs4646141", "rs142049267", "rs16979971", "rs12007623", "rs4646182", "rs147214574", "rs6632677", "rs139469582", "rs149000434", "rs148805807", "rs112032651", "rs144314464", "rs147077778", "rs182259051", "rs112621533", "rs35803318", "rs35304868", "rs113848176", "rs145345877", "rs12009805", "rs233570", "rs73635824", "rs73635823", "rs4646142", "rs4646157", "rs2074192", "rs79878075", "rs144239059", "rs67635467", "rs183583165", "rs137910448", "rs116419580", "rs2097723", "rs4646170")
for (snps in ace2_snps) {
  genotype_snps <- as.data.frame(bgen_ACE2$data[snps,,])
  idfromcsv <- read.csv("/Users/keeseyyyyy/Desktop/Walley/pospatid.csv")
  id <- as.character(idfromcsv[[1]])
  filtered_snps <- genotype_snps[id,] }

I need to run genotype_rs146217251 <- as.data.frame(bgen_ACE2$data["rs146217251",,]) for each rsID, and then I'd like to label the variable filtered_snps according to its rsID in the place of "snps" in the variable name for each variant.

I'm not very familiar with R syntax. Can anyone give me some tips?

For one variant, the process would go like this:

genotype_rs146217251 <- as.data.frame(bgen_ACE2$data["rs146217251",,])
idfromcsv <- read.csv("/Users/keeseyyyyy/Desktop/Walley/pospatid.csv")
id <- as.character(idfromcsv[[1]])
filtered <- genotype_rs146217251[id,]
psa
  • 25
  • 5
  • Please provide some reproducible code. We don't know how `pospatid.csv` looks like. – yrx1702 May 27 '20 at 19:14
  • @Mr.Zen It's just a column of values with `eid` as the column name and numbers like `1006573_1006573`. It stands for "positive patient ID's". So it's all the patients that are disease positive in my data. – psa May 27 '20 at 19:17
  • It looks like you access `idfromcsv` like a list object. In R, `[[1]]` is mainly used to refer to the first object in a list. Your question is more likely to get a useful answer if you provide some reproducible example that we can play around with. The bare minimum would be to provide an example of your goal and the error message that you get trying to get there. – yrx1702 May 27 '20 at 19:20
  • @Mr.Zen I provided an example of how I would do it with just one variant, `rs146217251`. I'm trying to automate this process with a for loop (or in some other way) to iterate through a list of variants. Does that help clarify what I'm trying to do? – psa May 27 '20 at 19:21
  • It is not clear how the objects that you use look like. A toy example of data that looks similar would be helpful to help you. See: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – yrx1702 May 27 '20 at 19:25

0 Answers0