dimanche 25 septembre 2016

Creating new data table based on ifelse on old data table

I'm trying to subset my data table using an ifelse statement, but I am not getting the result that I was looking for.

My initial data table looks like this:

head(Data_copy, n = 18)

    Company       Date       DOW variable value Year Month End_of_Month
 1:   ASXRI 1991-09-06    Friday       RI    NA 1991   Sep            0
 2:   ASXRI 1991-09-09    Monday       RI    NA 1991   Sep            0
 3:   ASXRI 1991-09-10   Tuesday       RI    NA 1991   Sep            0
 4:   ASXRI 1991-09-11 Wednesday       RI    NA 1991   Sep            0
 5:   ASXRI 1991-09-12  Thursday       RI    NA 1991   Sep            0
 6:   ASXRI 1991-09-13    Friday       RI    NA 1991   Sep            0
 7:   ASXRI 1991-09-16    Monday       RI    NA 1991   Sep            0
 8:   ASXRI 1991-09-17   Tuesday       RI    NA 1991   Sep            0
 9:   ASXRI 1991-09-18 Wednesday       RI    NA 1991   Sep            0
10:   ASXRI 1991-09-19  Thursday       RI    NA 1991   Sep            0
11:   ASXRI 1991-09-20    Friday       RI    NA 1991   Sep            0
12:   ASXRI 1991-09-23    Monday       RI    NA 1991   Sep            0
13:   ASXRI 1991-09-24   Tuesday       RI    NA 1991   Sep            0
14:   ASXRI 1991-09-25 Wednesday       RI    NA 1991   Sep            0
15:   ASXRI 1991-09-26  Thursday       RI    NA 1991   Sep            0
16:   ASXRI 1991-09-27    Friday       RI    NA 1991   Sep            0
17:   ASXRI 1991-09-30    Monday       RI    NA 1991   Sep            1
18:   ASXRI 1991-10-01   Tuesday       RI    NA 1991   Oct            0

This is 18 rows out of 250,000.

What i want is to split this data table based on an ifelse function as follows:

Data1 <- ifelse("Weekly" == "Weekly", Data_copy[End_of_Month ==1,], Data_copy)

*The "Weekly" == "Weekly" bit is going to be used in a function later.

I want Data1 to be a new data table which only contains the rows where End_of_Month ==1.

When i run the above code, I find that I get a list of the Company names and that is it.

I'll show you what the output looks like:

Data1[[1]]
    [1] "ASXRI" "ASXRI" "ASXRI" "ASXRI" "ASXRI" "ASXRI" "ASXRI" "ASXRI" "ASXRI" "ASXRI" "ASXRI"

Now if I scroll further down I get:

[1387] "AANRI" "AANRI" "AANRI" "AANRI" "AANRI" "AANRI" "APARI" "APARI" "APARI" "APARI" "APARI"
 [1398] "APARI" "APARI" "APARI" "APARI" "APARI" "APARI" "APARI" "APARI" "APARI" "APARI" "APARI"

Each of these entries is just one of the Company names.

I get my desired result if I do:

Data2 <- Data_copy[End_of_Month == 1, ]

Company       Date      DOW variable value Year Month End_of_Month
1:   ASXRI 1991-09-30   Monday       RI    NA 1991   Sep            1
2:   ASXRI 1991-10-31 Thursday       RI    NA 1991   Oct            1
3:   ASXRI 1991-11-29   Friday       RI    NA 1991   Nov            1
4:   ASXRI 1991-12-31  Tuesday       RI    NA 1991   Dec            1
5:   ASXRI 1992-01-31   Friday       RI    NA 1992   Jan            1
6:   ASXRI 1992-02-28   Friday       RI    NA 1992   Feb            1

Essentially I'd like to replicate Data2, but using an ifelse statement.

Aucun commentaire:

Enregistrer un commentaire