mardi 1 novembre 2016

Got an error using ifelse inside mutate inside the for loop

I have a list of 244 data frames which looks like the following: The name of the list is datas.

datas[[1]]

year  sal
2000 10000
2000 15000
2005 10000
2005 9000
2005 12000
2010 15000
2010 12000
2010 20000
2013 25000
2013 15000
2015 20000

I would like to make a new column called fix.sal, multiplying different values for different years. For example, I multiply 2 on sals which are on the same rows with 2000. In the same way, the number multiplied on the sal value is 1.8 for 2005, 1.5 for 2010, 1.2 for 2013, 1 for 2015. So the result should be like this:

Year  sal  fix.sal
2000 10000  20000
2000 15000  30000
2005 10000  18000
2005 9000   16200
2005 12000  21600
2010 15000  22500
2010 12000  18000
2010 20000  30000
2013 25000  30000
2013 15000  18000
2015 20000  20000

I succeeded to do this by using ifelse inside mutate which for package dplyr.

library(dplyr)
datas[[1]]<-mutate(datas[[1]], fix.sal=
ifelse(datas[[1]]$Year==2000,datas[[1]]$sal*2,
ifelse(datas[[1]]$Year==2005,datas[[1]]$sal*1.8,
ifelse(datas[[1]]$Year==2010,datas[[1]]$sal*1.5,
ifelse(datas[[1]]$Year==2013,datas[[1]]$sal*1.2,
datas[[1]]$sal*1)))))

But I have to do this operation to the 244 data frames in the list datas.

So I tried to do it using the for loop like this;

for(i in 1:244){
datas[[i]]<-mutate(datas[[i]], fix.sal=
    ifelse(datas[[i]]$Year==2000,datas[[i]]$sal*2,
    ifelse(datas[[i]]$Year==2005,datas[[i]]$sal*1.8,
    ifelse(datas[[i]]$Year==2010,datas[[i]]$sal*1.5,
    ifelse(datas[[i]]$Year==2013,datas[[i]]$sal*1.2,
    datas[[i]]$sal*1)))))
}

Then there came an error;

Error: invalid subscript type 'integer'

How can I solve this...?

Any comments will be greatly appreciated! :)

Aucun commentaire:

Enregistrer un commentaire