I've got a dataframe that includes columns like the one on the right here:

```
lengthArray speed_max
1 4 24, 18, 24, 18
2 10 2, 2, 2, 2, 2, 2, 2, 2, 2, 2
3 4 -999, -999, -999, -999
4 2 -999, -999
5 2 18, 18
6 1 -999
```

For these columns, I've written a function to extract the mean, median, max, or min from the array for each row of the dataframe, but I have a feeling this could be done much faster. Here's what I've got:

```
get_scalar <- function(name, to_return = 1)
{
vec_list = mydata[[name]]
alt_vector = vector(mode = "numeric", length = length(alt_max))
i = 1
# depending on what user wants, return max, min, mean or median
# for each array one per row
if(to_return == 0){
for(entry in alt_max){
alt_vector[i] = max(which(alt_max[i][[1]] != -999))
i = i + 1
}
}else if (to_return==1){
for(entry in alt_max){
alt_vector[i] = min(which(alt_max[i][[1]] != -999))
i = i + 1
}
}
...
#and repeated for two other cases
...
#then finally return the results as numeric vector
alt_vector = as.numeric(alt_vector)
}
```

The expected/desired output of this function is a numeric vector, with each row corresponding to the desired measurement for each array's row in the dataframe. So for example if I run `get_scalar("speed_max", to_return = 0)`

I would expect a numeric vector returned with the first rows being `(24, 2, NA....)`

based on the data I pasted above because max of the first row's "speed_max" array is 24 and max of the second row's "speed_max" array is 2 and the 3rd row does not contain any relevant data (-999 means omit).

I haven't been able to find a way to write this with sapply to access the first member of the list for each cell. For example the following syntax errors out:

```
> gg = max(mydata[[speed_max]][[1]])
Error in (function(x, i, exact) if (is.matrix(i)) as.matrix(x)[[i]] else .subset2(x, :
object 'speed_max' not found
```

And if I try an sapply rewrite like so, I can't seem to access the individual arrays per row. For example this function just prints out many 0's:

```
get_scalar_sapply <- function(name, to_return = 1)
{
vec_list = mydata[[name]]
alt_vector = vector(mode = "numeric", length = length(alt_max))
if(to_return == 1){
#alt_vector =sapply(alt_vector, function(x) max(which(x[[1]] != -999)))
alt_vector = sapply(alt_vector, function(x) print(x[[1]]))
}
alt_vector = as.numeric(alt_vector)
}
```

# Addendum, as requested output of dput(mydata)

```
> dput(head(mydata))
structure(list(endo = c(20216392L, 20167990L, 20211929L, 20214641L,
20206551L, 20178293L), lengthArray = c(4L, 10L, 4L, 2L, 2L, 1L
), sport = list(c(24, 18, 24, 18), c("2", "2", "2", "2", "2",
"2", "2", "2", "2", "2"), c("-999", "-999", "-999", "-999"),
c("-999", "-999"), c("18", "18"), "-999"), local_start_time = list(
c(NA_real_, NA_real_, NA_real_, NA_real_), c("u'2015-05-03T17:14:13.000Z'",
"u'2015-05-03T17:13:22.000Z'", "u'2015-05-03T17:14:13.000Z'",
"u'2015-05-03T17:13:22.000Z'", "u'2015-05-03T17:14:13.000Z'",
"u'2015-05-03T17:13:22.000Z'", "u'2015-05-03T17:14:13.000Z'",
"u'2015-05-03T17:13:22.000Z'", "u'2015-05-03T17:14:13.000Z'",
"u'2015-05-03T17:13:22.000Z'"), c("u'2015-02-25T10:02:10.000Z'",
"u'2015-02-02T22:37:34.000Z'", "u'2015-02-25T10:02:10.000Z'",
"u'2015-02-02T22:37:34.000Z'"), c("u'2015-02-02T18:28:23.000Z'",
"u'2015-02-02T18:28:23.000Z'"), c("u'2015-02-02T10:42:27.000Z'",
"u'2015-02-02T10:42:27.000Z'"), "u'2015-01-31T10:35:54.000Z'"),
distance = list(c(-999, 1.32598698139191, -999, 1.32598698139191
), c("-999", "-999", "-999", "-999", "-999", "-999", "-999",
"-999", "-999", "-999"), c("15.499165534973145", "-999",
"15.499165534973145", "-999"), c("6.071850776672363", "6.071850776672363"
), c("-999", "-999"), "-999"), duration = list(c(4, 1103,
4, 1103), c("8.0", "15.0", "8.0", "15.0", "8.0", "15.0",
"8.0", "15.0", "8.0", "15.0"), c("19492.0", "56.0", "19492.0",
"56.0"), c("1936.0", "1936.0"), c("3.0", "3.0"), "4083.49"),
speed_avg = list(c(-999, 4.32779069175962, -999, 4.32779069175962
), c("-999", "-999", "-999", "-999", "-999", "-999", "-999",
"-999", "-999", "-999"), c("2.862558789549729", "-999", "2.862558789549729",
"-999"), c("11.290631609514724", "11.290631609514724"), c("-999",
"-999"), "-999"), altitude_max = list(c(-999, 366, -999,
366), c("-999", "-999", "-999", "-999", "-999", "-999", "-999",
"-999", "-999", "-999"), c("335.5", "-999", "335.5", "-999"
), c("520.0", "520.0"), c("624.0", "624.0"), "-999"), altitude_min = list(
c(-999, 223, -999, 223), c("-999", "-999", "-999", "-999",
"-999", "-999", "-999", "-999", "-999", "-999"), c("-156.0",
"-999", "-156.0", "-999"), c("453.0", "453.0"), c("624.0",
"624.0"), "-999"), speed_max = list(c(-999, 5.01253,
-999, 5.01253), c("-999", "-999", "-999", "-999", "-999",
"-999", "-999", "-999", "-999", "-999"), c("66.8202", "-999",
"66.8202", "-999"), c("19.8268", "19.8268"), c("-999", "-999"
), "-999"), ascent = list(c(-999, 140, -999, 140), c("-999",
"-999", "-999", "-999", "-999", "-999", "-999", "-999", "-999",
"-999"), c("-999", "-999", "-999", "-999"), c("173.0", "173.0"
), c("-999", "-999"), "-999"), descent = list(c(-999, 272,
-999, 272), c("-999", "-999", "-999", "-999", "-999", "-999",
"-999", "-999", "-999", "-999"), c("-999", "-999", "-999",
"-999"), c("174.0", "174.0"), c("-999", "-999"), "-999"),
title = list(c(-999, -999, -999, -999), c("-999", "-999",
"-999", "-999", "-999", "-999", "-999", "-999", "-999", "-999"
), c("-999", "-999", "-999", "-999"), c("-999", "-999"),
c("-999", "-999"), "-999"), num_runs = c(0L, 0L, 0L,
0L, 0L, 0L), percent_runs = c(0, 0, 0, 0, 0, 0)), .Names = c("endo",
"lengthArray", "sport", "local_start_time", "distance", "duration",
"speed_avg", "altitude_max", "altitude_min", "speed_max", "ascent",
"descent", "title", "num_runs", "percent_runs"), row.names = c(NA,
6L), class = "data.frame")
```