dimanche 3 janvier 2016

Remove multiple columns and replace values of columns of dataframe based on condition in R

I have a huge dataframe with 4998 columns, column headers are the name of the companies. These columns contain stock prices as values in the column. So, I want to remove penny stocks that is price of stock(value in columns) less than 1.So, I want to remove the whole column if its values are less than 1. Additionally, there are columns in the data frame where the values fluctuate they go below 1 but then come back equal or greater to 1, in this scenerio I want in this column when value is below 1 it be replaced by NA. I have referred to 'Replace multiple values in multiple columns of dataframes with values in another column' but my case is bit different. I illustrate a small part of data frame

df1 <- Price
     Date        A   B       C
    01/01/2000  NA  0.03    0.95
    02/01/2000  NA  0.03    0.95
    03/01/2000  NA  0.03    0.95
    04/01/2000  NA  0.03    0.95
    05/01/2000  5   0.03    0.95
    06/01/2000  6   0.5       1
    07/01/2000  7   0.5       1
    08/01/2000  8   0.5       NA
    09/01/2000  9   0.5       NA

The desired output dataframe

df2<- df1
Date        A   C
01/01/2000  NA  NA
02/01/2000  NA  NA
03/01/2000  NA  NA
04/01/2000  NA  NA
05/01/2000  5   NA
06/01/2000  6   NA
07/01/2000  7   1
08/01/2000  8   1
09/01/2000  9   NA

Your help is highly appreciated.

Aucun commentaire:

Enregistrer un commentaire