Dear R Forum
I have a data frame as
beta_results = data.frame(instrument = c("ABC", "DEF",
"JKL", "LMN", "PQR", "STU",
"UVW", "XYZ"),
beta_values = c(1.27, -0.22, 0.529, 0.011, 2.31, -1.08, -2.7, 0.42))
> beta_results
instrument beta_values
1 ABC 1.270
2 DEF -0.220
3 JKL 0.529
4 LMN 0.011
5 PQR 2.310
6 STU -1.080
7 UVW -2.700
8 XYZ 0.420
Through some other process, I am getting instrument names as say (which may
change each time I run this process
and hence I can't hard code it).
instru = c("JKL", "STU", "XYZ")
Now I want the subset of beta_results, (say beta_results_A) pertaining to only
instru i.e
beta_results_A =
instrument beta_values
3 JKL 0.529
6 STU -1.080
8 XYZ 0.420
I did try
beta_results_A = beta_results[instru]
or
beta_results_A = subset(beta_results, beta_results$instrument = instru]
but I guess it's failing.
Kindly guide
Regards
Katherine
[[alternative HTML version deleted]]
Hi. Try this: beta_results[beta_results$instrument%in%instru, ] and see help page ?"%in%" Hope this helps Andrija On Thu, Jun 20, 2013 at 12:45 PM, Katherine Gobin <katherine_gobin@yahoo.com> wrote:> Dear R Forum > > I have a data frame as > > beta_results = data.frame(instrument = c("ABC", "DEF", "JKL", "LMN", > "PQR", "STU", "UVW", "XYZ"), > > beta_values = c(1.27, -0.22, 0.529, 0.011, 2.31, -1.08, -2.7, 0.42)) > > > beta_results > instrument beta_values > 1 ABC 1.270 > 2 DEF -0.220 > 3 JKL 0.529 > 4 LMN 0.011 > 5 PQR 2.310 > 6 STU -1.080 > 7 UVW -2.700 > 8 XYZ 0.420 > > > Through some other process, I am getting instrument names as say (which > may change each time I run this process > and hence I can't hard code it). > > > instru = c("JKL", "STU", "XYZ") > > Now I want the subset of beta_results, (say beta_results_A) pertaining to > only instru i.e > > beta_results_A > > > instrument beta_values > 3 JKL 0.529 > 6 STU -1.080 > 8 XYZ 0.420 > > > I did try > > beta_results_A = beta_results[instru] > or > beta_results_A = subset(beta_results, beta_results$instrument = instru] > > but I guess it's failing. > > Kindly guide > > Regards > > Katherine > [[alternative HTML version deleted]] > > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > >[[alternative HTML version deleted]]
HI,
You could also use:
beta_results[!is.na(match(beta_results[,1],instru)),]
#? instrument beta_values
#3??????? JKL?????? 0.529
#6??????? STU????? -1.080
#8??????? XYZ?????? 0.420
#If there are no duplicates, this could also work
beta_results[match(instru,beta_results[,1]),]
#? instrument beta_values
#3??????? JKL?????? 0.529
#6??????? STU????? -1.080
#8??????? XYZ?????? 0.420
A.K.
----- Original Message -----
From: Katherine Gobin <katherine_gobin at yahoo.com>
To: "r-help at r-project.org" <r-help at r-project.org>
Cc:
Sent: Thursday, June 20, 2013 6:45 AM
Subject: [R] Choosing subset of data.frame
Dear R Forum
I have a data frame as
beta_results = data.frame(instrument = c("ABC", "DEF",
"JKL",? "LMN", "PQR", "STU",
"UVW", "XYZ"),
beta_values = c(1.27, -0.22, 0.529, 0.011, 2.31, -1.08, -2.7, 0.42))
> beta_results
? instrument beta_values
1??????? ABC?????? 1.270
2??????? DEF????? -0.220
3??????? JKL?????? 0.529
4??????? LMN?????? 0.011
5??????? PQR?????? 2.310
6??????? STU????? -1.080
7??????? UVW????? -2.700
8??????? XYZ?????? 0.420
Through some other process, I am getting instrument names as say (which may
change each time I run this process
and hence I can't hard code it).
instru = c("JKL", "STU", "XYZ")
Now I want the subset of beta_results, (say beta_results_A)? pertaining to only
instru i.e
beta_results_A =
? instrument beta_values
3??????? JKL?????? 0.529
6??????? STU????? -1.080
8??????? XYZ?????? 0.420
I did try
beta_results_A = beta_results[instru]
or
beta_results_A = subset(beta_results, beta_results$instrument = instru]
but I guess it's failing.
Kindly guide
Regards
Katherine
??? [[alternative HTML version deleted]]
______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.