How to add together 2 columns of a list?

i have 2 lists and i want to add together the corresponding rows like as in matrix addition.

midpoint_system = function(f, a, b, x0, N){
h = (b-a)/N
time_seq = seq(a, b, h)
hat_x = matrix(0, ncol = length(x0), nrow = (N+1))
hat_x[1,] = x0
for (i in 1:N){
w = hat_x[i,] + hf(time_seq[i], hat_x[i,])/2
hat_x[i+1,] = hat_x[i,] + h
f(time_seq[i]+h/2, w)
}
return(list(t=time_seq, x=hat_x))
return(c(0,list(h)))
return(list(((t=time_seq[i]) + (x=hat_x[i]))*-1))
}

this is the code and as you can see in the first return i have a list with 2 columns and i want to add these together.

Lists are generally for storing a set of objects that aren't the same type. For that reason, you can't do math with lists.

I recommend instead returning instead a data frame with a column for each of your variables. The data frame columns can be numeric type, and these will be easy to add.

how could i do this and then return a data frame of the new column of the summed rows?

result <- data.frame(
  x = ...,
  y = ...,
)
result$sum_xy = result$x + result$y
return(result)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.