So I know that double brackets return the element only while single brackets return a list with that element inside. But how does this matter functionally when making functions or for loops?
Say I have a set of X and Y values, and want to find the best slope and y intersect for them. I have a list of x and y values ( stored in data1) and a list of yintersect and slope values (stored in parameters).
I want to see which set of y-intersect and slope values comes closest to the actual Y values. So first I will make a function which outputs the y value GIVEN a set of yintersect and slope values from the parameters list. This function is showed below titled modelling_function
parameters <- list(yintersect = c(1:10), slope = c(5:14))
data1 <- list(x = sample(100, 10), y = sample(200:10))
modelling_function <- function(parameters, data1) {
parameters[[1]]+ parameters[[2]] * data1$x}
So two questions...
- What is the difference between using single and double brackets here?
- Why when I run this function do I only get 10 values? I would expect to get 10 * 10 = 100 values, because for each x value, there are 10 combinations of slope and intersect to be used.
I know im missing something here, I think related to what exactly is being done to each vector/list. I hope the brackets and knowing what is being done to each subsetted list will help me figure that out.