Jason
Jason

Reputation: 311

subsetting error in R

I have a large dataframe called dualbeta which contains 2 rows and 6080 columns. Here is a sample:

row.names     A.Close   AA.Close    AADR.Close  AAIT.Close  AAL.Close
1   upside    1.253929  0.9869027   0.6169613   0.6353903   0.1782124
2   downside  1.027412  1.1936236   0.5915299   0.5697878   0.1702382

I am trying to extract only those with the upside >= 1.00 and those with a downside <=1.00. I used combinations <- subset(dualbeta, upside>=1.00 & downside<=1.00) but i get the following:

row.names   A.Close     AA.Close     AADR.Close  AAIT.Close 
1 NA          NA          NA            NA          NA  
2 NA.1        NA          NA            NA          NA  
3 NA.2        NA          NA            NA          NA  
4 NA.3        NA          NA            NA          NA  
5 NA.4        NA          NA            NA          NA  
...

It should just return a 2 by x table where x is the number of combinations found. I do not know why I am getting a bunch of rows? Additionally, i thought i had NA values in the dualbeta so i used na.omit(dualbeta)->dualbeta but it deleted everything & turned dualbeta into a 0 by 6080. I also used which(is.na(dualbeta)) which returned 3307 and 3308 but when i checked those columns, they did not contain NAs.

Upvotes: 1

Views: 2015

Answers (2)

MrFlick
MrFlick

Reputation: 206242

You can the data with simple indexing.

Let's say this is your data

dualbeta<-data.frame(matrix(runif(24,0,2), 
    nrow=2, 
    dimnames=list(c("upside","downside"), letters[1:12])))

then you can extract with

dualbeta[, dualbeta[1,]>=1.00 & dualbeta[2,]<=1.00]

Upvotes: 0

Matthew Lundberg
Matthew Lundberg

Reputation: 42659

You might work on the transpose of the data in order to select rows with the proper characteristics (which are columns in the transpose):

# Fix up the data, use proper row names
rownames(x) <- x$row.names
# Remove old row name column
x <- x[-1]

# transpose and subset
subset(data.frame(t(x)), upside > 1 & downside < 1)

This expression returns a zero-length result with your example data. Changing the parameters shows what is returned:

subset(data.frame(t(x)), upside > .6 & downside < .6)
##               upside  downside
## AADR.Close 0.6169613 0.5915299
## AAIT.Close 0.6353903 0.5697878

Upvotes: 2

Related Questions