Reputation: 125
I'm starting with a df that looks like this:
sample_id target_id length eff_length est_counts tpm class
1 SRR3884838C (A)n 69 70 0 0.00000 0
2 SRR3884838C (AC)n 69 70 0 0.00000 0
3 SRR3884838C (AG)n 69 70 0 0.00000 0
4 SRR3884838C (AT)n 69 70 5 15.98870 0
I would like to use dplyr's select function to select only sample_ids that end with the letter C, as well as the target_id and tpm.
Example data:
> dput(droplevels(head(te,4)))
structure(list(sample_id = structure(c(1L, 1L, 1L, 1L), .Label = "SRR3884838C", class = "factor"),
target_id = structure(1:4, .Label = c("(A)n", "(AC)n", "(AG)n",
"(AT)n"), class = "factor"), length = c(69L, 69L, 69L, 69L
), eff_length = c(70L, 70L, 70L, 70L), est_counts = c(0,
0, 0, 5), tpm = c(0, 0, 0, 15.9887), class = c(0L, 0L, 0L,
0L)), .Names = c("sample_id", "target_id", "length", "eff_length",
"est_counts", "tpm", "class"), row.names = c(NA, 4L), class = "data.frame")
I tried using the following:
teC <- select(te, (sample_id, ends_with("C")), target_id, tpm)
Which gives me the sample_id, target_id and tpm but does not select only the sample_id's that end with C eg:
sample_id target_id tpm
9759 SRR3884843CxS Tigger15a 0.00000e+00
9760 SRR3884843CxS Tigger16a 0.00000e+00
9761 SRR3884843CxS Tigger16b 0.00000e+00
Am I doing something wrong with select? I was able to work on example data from a tutorial site without issue.
Upvotes: 1
Views: 4618
Reputation: 5225
select
is used to keep variables (read: columns) by name, which is what you're doing with sample_id
, target_id
, and tmp
. If you want to further filter by the values within sample_id
, then add filter
:
teC <- te %>% select(sample_id, target_id, tpm) %>% filter(grepl("C$", sample_id))
The regular expression "C$"
will match strings that end with "C"; "CxS$"
will match strings ending with "CxS"; and "(C|CxS)$"
will match both.
Upvotes: 4