mardi 22 mai 2018

Lapply with ifelse condition

I've a dataframe which contains 40 rows and 10 columns. I want to derive a column if index of rows less than 17, new column is called Prediction equals to 'Value' column. What I mean is that:

ID  Year    Value
1   2016    114235
2   2016    114235
3   2016    114235
4   2016    114235
5   2016    114235

Then:

ID  Year    Value   Prediction
1   2016    114235  114235
2   2016    114235  114235
3   2016    114235  114235
4   2016    114235  114235
5   2016    114235  114235

I tried to code as follows and all rowns of new column was 'NA'.

newdata$Prediction <- ifelse(nrow(newdata) <= 17, newdata$Value, NA)

newdata$Prediction <- lapply(newdata, function(x) ifelse(nrow(newdata) <= 17, newdata$Value, NA))

It doesn't work. How can I do that?

Aucun commentaire:

Enregistrer un commentaire