简体   繁体   中英

R:Extract values from the vector and use them in function to calculate the difference

i would like to extract specific values from the vector and then use them in my function. I am going to explain it on the example.

This all calculations are taking place in shiny app , this example is a simplified version.

At the moment...

I calculate the time difference of the each sample (the samples can be differentiated using ID column) between two temperature points for x axis (in the example below x1=600 --> it is a reactive value in my shiny app and x2=800 --> it is a constant value).

####Sample data
data <- structure(list(id = c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,2L, 2L),
               Zeit = c(0L, 180L, 360L, 420L, 600L, 604L, 0L, 180L,360L, 480L, 600L,
                        605L), Temp = c(963L, 824L, 666L, 658L, 641L,549L, 957L, 823L, 661L,
                                        660L, 642L, 562L)), .Names = c("id","Zeit", "Temp"), row.names = c(NA,
                                                                                                           12L), class = "data.frame")
####Calculating the time for x=600 (in my app it is a reactive value)
Zt <- vapply(unique(data$id), function(a){
  with(data[data$id == a,], approx(x = Temp, y = Zeit, xout = paste(600)))$y ###in my original data it is a reactive value:paste(input$t8xzeit)
}, double(1))
datat5 <- data.frame(ID = unique(data$id), Zeit= Zt)
####Calculating the time for x=800
Zt2 <- vapply(unique(data$id), function(a){
  with(data[data$id == a,], approx(x = Temp, y = Zeit, xout = paste(800)))$y
}, double(1))
datat8 <- data.frame(ID = unique(data$id), Zeit2= Zt2)
####Merging the data
datat85 <- merge(datat5,datat8,by="ID")
####Calculating the difference
datat85$delta <- (datat85$Zeit - datat85$Zeit2)

What i need to do...

if we have x1=600 (which is reactive value) and x2=800 , and we want all the values between 600 and 800 by 50, we get: 600,650,700,750,800. Then i need to use every each of this value in my function: Zt (look in the piece of code above) as xout =... and create for each calculated value a new column.

Is there a way to make it somehow automatic (loop?)?It is not possible to write each time new Zt function, as the number of values in vector might change due to the reactive x1 value.

Thanks very much for help!

you can add the columns for a given list of vals by doing the following:

data.new <- data.frame(ID = unique(data$id))
vals <- seq(600,800,by=50)
data.new[paste("Zeit",vals,sep="")] <- sapply(vals,function(xout.input) {
  vapply(unique(data$id), function(a){
    with(data[data$id == a,], approx(x = Temp, y = Zeit, xout = xout.input))$y
  }, double(1))
})

This works by using sapply to apply Zt for all the desired values creating a matrix whose columns contain the results. These are stored in the new frame by assigning the matrix to the columns with the names given by paste("Zeit",vals,sep="") .

Then

> data.new
  ID  Zeit600  Zeit650  Zeit700  Zeit750  Zeit800
1  1 601.7826 504.7059 321.2658 264.3038 207.3418
2  2 602.6250 546.6667 316.6667 261.1111 205.5556
> datat85$Zeit == data.new$Zeit600
[1] TRUE TRUE
> datat85$Zeit2 == data.new$Zeit800
[1] TRUE TRUE

I hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM