Home > Back-end >  Subset rows of a table if they contain a specific character vector
Subset rows of a table if they contain a specific character vector

Time:05-07

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.

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"

CodePudding user response:

You can use the package dplyr to achieve that:

library(dplyr)

N_JN5_X = N_JN5_X %>% 
filter(Target == "HK")
  • Related