How to extract rows from my data frame without having a data format conversion?

Multi tool use
How to extract rows from my data frame without having a data format conversion?
I would like to extract rows from a big dataset, named data1.
My destination is to add a new column next to the Value of the extracted data1.
Before that, I need to extract some GeneSymbols and then I would like to make a new column in the extracted data frame.
Let's hypothetically say if this is my data.
GeneSymbol ID Value
let-7 A 0.5
miR-16 A 0.7
miR-19 A 0.3
miR-21 A 0.2
add1 = 1
data11 = cbind(data1, add1)
head(data11)
GeneSymbol ID Value add1
let-7 A 0.5 1
miR-16 A 0.7 1
miR-19 A 0.3 1
miR-21 A 0.2 1
It works well if I do not extract rows from my data set.
However, I am in trouble if I do like this.
newdata1 = data1[c("let-7", "miR-19"),]
head(newdata1)
GeneSymbol ID Value
<NA> <NA> NA
<NA> <NA> NA
Why my data became NAs?
I got stuck.
Since my real data is big, over 30,000 rows.
Also, my target rows of interest locate randomly, so I definitely want to sort by GeneSymbol, not a row's number.
1 Answer
1
We can use %in%
to get a logical vector for subsetting the rows
%in%
data1[data1$GeneSymbol %in% c("let-7", "miR-19"),]
# GeneSymbol ID Value
#1 let-7 A 0.5
#3 miR-19 A 0.3
The "let-7", "miR-19"
are elements in the 'GeneSymbol` and it is not row.names. So, using that in the row index get NA rows as those are in the original dataset
"let-7", "miR-19"
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.