Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Subset rows of a table if they contain a specific character vector

I know this is a common question and I’ve read through many other stackoverflow entries but I am still stuck, apologies! I’m a beginner to R (still!).

I have an exported csv file that I want to extract certain data from so I can calculate stuff from the X values.

Stuck quite far at the beginning… at the moment I want this table to only contain the rows that have "HK" in the "Target" column.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

This is my code:

#setwd("~/R")
#N_JN5 <- read.table("filename.csv", skip=22, header=TRUE, sep=",")
#N_JN5_X <- N_JN5[c(-1,-2,-3,-6,-7,-8,-9,-10,-11,-12,-14,-17,-18,-19,-20,-21)]
#table(N_JN5_X$heading)

Layout of my table:

> head(N_JN5_X)
  Sample   Target     X                X.Mean     X.SD
1  ABCD     Gene       Undetermined      NA        NA
2  ABDC     HK        73.2374         82.3615 0.28934
3  ABCD     Gene       Undetermined      NA        NA
4  ABCD     HK        78.9349         82.3615 0.27414
5  ABCD     Gene       Undetermined      NA        NA
6  ABCD     HK        94.9123         82.3615 0.28234

> colnames(N_JN5_X)
[1] "Sample"  "Target"  "X"      "X.Mean" "X.SD"

>Solution :

You can use the package dplyr to achieve that:

library(dplyr)

N_JN5_X = N_JN5_X %>% 
filter(Target == "HK")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading