condtional computation based on two dataframes in R -


i performing computation involves 2 data frames.i created 2 reproducible examples of 2 data frames example

> df1 day1 day2 day3 day4 day5 day6 day7 day8 day9 day10 time1 0.03 0.43 0.39 0.41 0.94 0.70 0.18 0.65 0.72 0.72 time2 0.42 0.63 0.93 0.53 0.19 0.55 0.22 0.16 0.56 0.04

and

> df2 day time x3 x4 x5 1 1 1 9.252042 19.512621 11.601671 2 1 2 5.021522 17.712484 5.044728 3 2 1 9.603795 19.404302 17.206771 4 2 2 19.686793 18.791541 12.655874 5 3 1 7.546551 18.810526 19.865979 6 3 2 18.233872 19.596584 11.653980 7 4 1 17.499680 14.014276 15.553013 8 4 2 8.115352 17.898786 12.841630 9 5 1 10.719540 8.518823 19.126440 10 5 2 12.853401 6.026599 14.041490 11 6 1 19.984946 10.693528 6.890835 12 6 2 16.360035 15.778092 18.087471 13 7 1 15.498714 15.039444 5.259257 14 7 2 13.179111 17.533358 7.382507 15 8 1 5.124188 15.507194 12.547365 16 8 2 8.008336 10.463382 6.934014 17 9 1 11.246527 6.975527 14.464758 18 9 2 17.914083 18.039384 19.324091 19 10 1 9.876625 19.216317 8.787550 20 10 2 11.851955 15.729080 5.741095

the columns in df1 represent days values recorded , rows indicate hours/or time (time 1 or 2). in df2, first 2 columns represent days , times respectively , other columns locations data recorded.

what r create data frame has same size df2, divides values in df2[,3:5] corresponding df1 value i.e depending on values in day , time columns of df2, select corresponding values of df1. example for first value of df2$x3, in new data frame have value of 9.252042 divided 0.03. , third value of df2$x3, have value of 9.603795 divided 0.43.

thank in advance help!

i suppose makes data (df1 , df2) below:

df1 = data.frame(time=c(1:10),time1=c(0.03,0.43,0.39,0.41,.94,.70,.18,.065,0.72,0.72),time2 = c(.42,.63,.93,.53,.19,.55,.22,.16,.56,.04)) df2 = data.frame(day = rep(c(1:10),each=2),time = rep(c(1,2),10),x3=c(9.2,5.02,9.6,19.6,7.5,18.2,17.4,8.1,10.7,12.8,19.9,16.3,15.4,13.1,5.1,8,11.2,17.9,9.8,11.8),x4=c(19.5,17.7,19.4,18.8,18,19.5,14.01,17.8,8.5,6,10.6,15.7,15,17.5,15,10,6,18,19,15),x5=c(11.6,5,17,12,19,11,15,12,19,14,6,18,5,7,12,6,14,19,8,5)) 

then code new create df3 this:

df3 = data.frame(df2$day,df2$time,newx3 = df2$x3 / df1$time[df2$day],newx4 = df2$x4 / df1$time[df2$day],newx5 = df2$x5 / df1$time[df2$day]) 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -