lundi 29 août 2016

apply function with a conditional statement using two dataframe

I have two data frame x and y.

x<-data.frame("j"=c("A","B","C"),"k"=c(1,90,14))
  x j  k
  1 A  1
  2 B 90
  3 C 14

y<-data.frame("A"=c(1,0,0,1,1),"B"=c(0,1,0,0,1),"C"=c(1,1,1,0,0))
  A B C
1 1 0 1
2 0 1 1
3 0 0 1
4 1 0 0
5 1 1 0

I need a function with a conditional statement where if in the data set y there is a 0 a replace by -1 or 1 replace by 1 in the column A and so on to get this result.

z<-data.frame("A"=c(1,-1,-1,1,1),"B"=c(-90,90,-90,-90,90),"C"=c(14,14,14,-14,-14))
   A   B   C
1  1 -90  14
2 -1  90  14
3 -1 -90  14
4  1 -90 -14
5  1  90 -14

Aucun commentaire:

Enregistrer un commentaire