lundi 21 janvier 2019

Ifelse retrieving a specific number of NAs

I want to obtain a data.frame with a specific number of rows based on two dfs using the ifelse option in BaseR.

So far, I have tried with:

Plan = data.frame(Plan = c(NA, 'Tax', 'Type', NA, ifelse(is.na(c(df1$TAX1, df2$TAX2)),
               c(rep(NA, length(unique(df1$CODE))),
                 rep(NA, length(df2$CODE))),
               c(rep(unique(df1$TAX1), length(unique(df1$CODE))),
                 rep(unique(df2$TAX2), length(df2$CODE)))))

But somehow, I get a large dataset considering all values and not just unique code values.

df1 looks like this:

TAX1    CODE
15       1
15       3
15       2
15       1
15       1

where code values 1, 2 and 3 are unique.

df2 looks like this:

TAX2    CODE
5        11
5        12
5        13
5        14
5        15
5        11
5        10

Within df2 unique code values differ.

The output I am getting with my code is:

Plan
NA
Tax
Type
NA
15
15
15
15
15
5
5
5
5
5
5
5

But I want to obtain an output where the TAX# is repeating itself the number of times CODE is unique in df1, like this:

Plan
NA
Tax
Type
NA
15
15
15
5
5
5
5
5
5
5

What else can I try? In case there is no data, NA for instance, then I would like to fill the output df with NA the number of times CODE is unique, that is why I am using ifelse.

Preferably BaseR, please!

Aucun commentaire:

Enregistrer un commentaire