0

I have a data frame in R which, when running data$data['rs146217251',1:10,] looks something like:

                g=0 g=1 g=2
1389117_1389117   0   1  NA
2912943_2912943   0   0   1
3094358_3094358   0   0   1
5502557_5502557   0   0   1
2758547_2758547   0   0   1
3527892_3527892   0   1  NA
3490518_3490518   0   0   1
1569224_1569224   0   0   1
4247075_4247075   0   1  NA
4428814_4428814   0   0   1

The leftmost column are participant identifiers. There are roughly 500,000 participants listed in this data frame, but I have a subset of them (about 5,000) listed by their identifiers. What is the best way to go about extracting only these rows that I care about according to their identifier in R or python (or some other way)?

1
  • Are the participant identifiers rownames or a separate column with a header? May 23, 2020 at 0:46

1 Answer 1

0

Assuming that the participant identifiers are row names, you can filter by the vector of the identifiers as below:

df <- data$data['rs146217251',1:10,]

#Assuming the vector of identifiers
id <- c("4428814_4428814", "3490518_3490518", "3094358_3094358")

filtered <-  df[id,]

Output:

> filtered
                g.0 g.1 g.2
4428814_4428814   0   0   1
3490518_3490518   0   0   1
3094358_3094358   0   0   1
3
  • This worked when I tried your exact code, but when I try to make an identifier column with around 1800 values using the same format as id <- c("4428814_4428814", "3490518_3490518", "3094358_3094358"), it gives me the + sign like it's waiting for more input. How do I fix this?
    – psa
    May 23, 2020 at 20:47
  • This is the beginning: i.imgur.com/xSCrMpv.png and the end: i.imgur.com/ztwOD7P.png of the command. I'm not sure if I'm typing something incorrectly, or if Rstudio just doesn't like columns to be that large, or if something else is going wrong?
    – psa
    May 23, 2020 at 20:50
  • This will happen when you open a round bracket and not closing it. Please check whether you have any unwanted brackets in your code May 24, 2020 at 14:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.