Combine lists with keeping same structure and names [duplicate] - r

This question already has answers here:
Can lists be created that name themselves based on input object names?
(4 answers)
Closed 2 years ago.
I have several lists (ListA, ListB, ListC...) with the same internal structure as the example below. I would like to combine all of them, keeping their structure, and have one list with all lists (ListAll). How can I do this?
Example:
I have:
ListA
$ data :'data.frame': 1 obs. of 2 variables:
..$ mean: num -0.128
..$ sd : num 1.11
$ simulations :'data.frame': 1000 obs. of 2 variables:
..$ mean: num [1:1000] -0.0116 -0.0156 0.0336 -0.0502 -0.0427 ...
..$ sd : num [1:1000] 1.003 1.014 0.963 1.036 1.051 ...
$ values:'data.frame': 35 obs. of 2 variables:
..$ C: num [1:35] 3.45 2.91 2.62 2.06 1.87 ...
..$ D: num [1:35] 5.42 2.89 3.34 1.68 1.43 ...
and several lists with the same structure.
I would like to get:
ListAll
$ ListA
$ data :'data.frame': 1 obs. of 2 variables:
..$ mean: num -0.128
..$ sd : num 1.11
$ simulations :'data.frame': 1000 obs. of 2 variables:
..$ mean: num [1:1000] -0.0116 -0.0156 0.0336 -0.0502 -0.0427 ...
..$ sd : num [1:1000] 1.003 1.014 0.963 1.036 1.051 ...
$ values:'data.frame': 35 obs. of 2 variables:
..$ C: num [1:35] 3.45 2.91 2.62 2.06 1.87 ...
..$ D: num [1:35] 5.42 2.89 3.34 1.68 1.43 ...
$ ListB
$ data :'data.frame': 1 obs. of 2 variables:
..$ mean: num -0.132
..$ sd : num 1.01
$ simulations :'data.frame': 1000 obs. of 2 variables:
..$ mean: num [1:1000] -0.0114 -0.0123 0.0378 -0.0102 -0.0340 ...
..$ sd : num [1:1000] 1.013 1.011 0.876 1.012 1.023 ...
$ values:'data.frame': 35 obs. of 2 variables:
..$ C: num [1:35] 4.41 1.61 1.42 1.96 2.07 ...
..$ D: num [1:35] 2.41 2.19 2.54 2.08 2.53 ...
** and names(listAll) would be:**
ListaA, ListB, ListC...

You can create a list of lists in base R.
ListAll <- list(ListA, ListB, ListC)

Related

Take 20+ subsets of data?

I have a dataset and would like to take a lot of subsets based on various columns, values, and conditional operators. I think the most desirable output is a list containing all of these subsetted data frames as separate elements in the list. I attempted to do this by building a data frame that contains the subset conditions I would like to use, building a function, then using apply to feed that data frame to the function, but that didn't work. I'm sure there's probably a better method that uses an anonymous function or something like that, but I'm not sure how I would implement that. Below is an example code that should produce 8 subsets of data.
Original dataset, where x1 and x2 are scored on items that won't be used for subsetting and RT and LS are the variables that will be a subset on:
df <- data.frame(x1 = rnorm(100),
x2 = rnorm(100),
RT = abs(rnorm(100)),
LS = sample(1:10, 100, replace = T))
Dataframe containing the conditions for subsetting. E.g., the first subset of data should be any observations with values greater than or equal to 0.5 in the RT column, the second subset should be any observations greater than or equal to 1 in the subset column, etc. There should be 8 subsets, 4 done on the RT variable and 4 done on the LS variable.
subsetConditions <- data.frame(column = rep(c("RT", "LS"), each = 4),
operator = rep(c(">=", "<="), each = 4),
value = c(0.5, 1, 1.5, 2,
9, 8, 7, 6))
And this is the ugly function I wrote to attempt to do this:
subsetFun <- function(x){
subset(df, eval(parse(text = paste(x))))
}
subsets <- apply(subsetConditions, 1, subsetFun)
Thanks for any help!
Consider Map (wrapper to mapply) without any eval + parse. Since ==, <=, >=, and other operators can be used as functions with two arguments where 4 <= 5 can be written as `<=`(4,5) or "<="(4, 5), simply pass arguments elementwise and use get to reference the function by string:
sub_data <- function(col, op, val) {
df[get(op)(df[[col]], val),]
}
sub_dfs <- with(subsetConditions, Map(sub_data, column, operator, value))
Output
str(sub_dfs)
List of 8
$ RT:'data.frame': 62 obs. of 4 variables:
..$ x1: num [1:62] -1.12 -0.745 -1.377 0.848 1.63 ...
..$ x2: num [1:62] -0.257 -2.385 0.805 -0.313 0.662 ...
..$ RT: num [1:62] 0.693 1.662 0.731 2.145 0.543 ...
..$ LS: int [1:62] 5 5 1 2 9 1 5 9 3 10 ...
$ RT:'data.frame': 36 obs. of 4 variables:
..$ x1: num [1:36] -0.745 0.848 0.908 -0.761 0.74 ...
..$ x2: num [1:36] -2.3849 -0.3131 -2.4645 -0.0784 0.8512 ...
..$ RT: num [1:36] 1.66 2.15 1.74 1.65 1.13 ...
..$ LS: int [1:36] 5 2 1 5 9 10 2 7 1 3 ...
$ RT:'data.frame': 14 obs. of 4 variables:
..$ x1: num [1:14] -0.745 0.848 0.908 -0.761 -1.063 ...
..$ x2: num [1:14] -2.3849 -0.3131 -2.4645 -0.0784 -2.9886 ...
..$ RT: num [1:14] 1.66 2.15 1.74 1.65 2.63 ...
..$ LS: int [1:14] 5 2 1 5 5 6 9 4 8 4 ...
$ RT:'data.frame': 3 obs. of 4 variables:
..$ x1: num [1:3] 0.848 -1.063 0.197
..$ x2: num [1:3] -0.313 -2.989 0.709
..$ RT: num [1:3] 2.15 2.63 2.05
..$ LS: int [1:3] 2 5 6
$ LS:'data.frame': 92 obs. of 4 variables:
..$ x1: num [1:92] -1.12 -0.745 -1.377 0.848 0.612 ...
..$ x2: num [1:92] -0.257 -2.385 0.805 -0.313 0.958 ...
..$ RT: num [1:92] 0.693 1.662 0.731 2.145 0.489 ...
..$ LS: int [1:92] 5 5 1 2 1 9 1 5 9 3 ...
$ LS:'data.frame': 78 obs. of 4 variables:
..$ x1: num [1:78] -1.12 -0.745 -1.377 0.848 0.612 ...
..$ x2: num [1:78] -0.257 -2.385 0.805 -0.313 0.958 ...
..$ RT: num [1:78] 0.693 1.662 0.731 2.145 0.489 ...
..$ LS: int [1:78] 5 5 1 2 1 1 5 3 5 2 ...
$ LS:'data.frame': 75 obs. of 4 variables:
..$ x1: num [1:75] -1.12 -0.745 -1.377 0.848 0.612 ...
..$ x2: num [1:75] -0.257 -2.385 0.805 -0.313 0.958 ...
..$ RT: num [1:75] 0.693 1.662 0.731 2.145 0.489 ...
..$ LS: int [1:75] 5 5 1 2 1 1 5 3 5 2 ...
$ LS:'data.frame': 62 obs. of 4 variables:
..$ x1: num [1:62] -1.12 -0.745 -1.377 0.848 0.612 ...
..$ x2: num [1:62] -0.257 -2.385 0.805 -0.313 0.958 ...
..$ RT: num [1:62] 0.693 1.662 0.731 2.145 0.489 ...
..$ LS: int [1:62] 5 5 1 2 1 1 5 3 5 2 ...
You were actually pretty close with your function, but just needed to make an adjustment. So, with paste for each row, you need to collapse all 3 columns so that it is only 1 string rather than 3, then it can properly evaluate the expression.
subsetFun <- function(x){
subset(df, eval(parse(text = paste(x, collapse = ""))))
}
subsets <- apply(subsetConditions, 1, subsetFun)
Output
Then, it will return the 8 subsets.
str(subsets)
List of 8
$ :'data.frame': 67 obs. of 4 variables:
..$ x1: num [1:67] -1.208 0.606 -0.17 0.728 -0.424 ...
..$ x2: num [1:67] 0.4058 -0.3041 -0.3357 0.7904 -0.0264 ...
..$ RT: num [1:67] 1.972 0.883 0.598 0.633 1.517 ...
..$ LS: int [1:67] 8 9 2 10 8 5 3 4 7 2 ...
$ :'data.frame': 35 obs. of 4 variables:
..$ x1: num [1:35] -1.2083 -0.4241 -0.0906 0.9851 -0.8236 ...
..$ x2: num [1:35] 0.4058 -0.0264 1.0054 0.0653 1.4647 ...
..$ RT: num [1:35] 1.97 1.52 1.05 1.63 1.47 ...
..$ LS: int [1:35] 8 8 5 4 7 3 1 6 8 6 ...
$ :'data.frame': 16 obs. of 4 variables:
..$ x1: num [1:16] -1.208 -0.424 0.985 0.99 0.939 ...
..$ x2: num [1:16] 0.4058 -0.0264 0.0653 0.3486 -0.7562 ...
..$ RT: num [1:16] 1.97 1.52 1.63 1.85 1.8 ...
..$ LS: int [1:16] 8 8 4 6 10 2 6 6 3 9 ...
$ :'data.frame': 7 obs. of 4 variables:
..$ x1: num [1:7] 0.963 0.423 -0.444 0.279 0.417 ...
..$ x2: num [1:7] 0.6612 0.0354 0.0555 0.1253 -0.3056 ...
..$ RT: num [1:7] 2.71 2.15 2.05 2.01 2.07 ...
..$ LS: int [1:7] 2 6 9 9 7 7 4
$ :'data.frame': 91 obs. of 4 variables:
..$ x1: num [1:91] -0.952 -1.208 0.606 -0.17 -0.048 ...
..$ x2: num [1:91] -0.645 0.406 -0.304 -0.336 -0.897 ...
..$ RT: num [1:91] 0.471 1.972 0.883 0.598 0.224 ...
..$ LS: int [1:91] 6 8 9 2 1 8 4 5 3 4 ...
$ :'data.frame': 75 obs. of 4 variables:
..$ x1: num [1:75] -0.952 -1.208 -0.17 -0.048 -0.424 ...
..$ x2: num [1:75] -0.6448 0.4058 -0.3357 -0.8968 -0.0264 ...
..$ RT: num [1:75] 0.471 1.972 0.598 0.224 1.517 ...
..$ LS: int [1:75] 6 8 2 1 8 4 5 3 4 1 ...
$ :'data.frame': 65 obs. of 4 variables:
..$ x1: num [1:65] -0.9517 -0.1698 -0.048 0.2834 -0.0906 ...
..$ x2: num [1:65] -0.645 -0.336 -0.897 -2.072 1.005 ...
..$ RT: num [1:65] 0.471 0.598 0.224 0.486 1.053 ...
..$ LS: int [1:65] 6 2 1 4 5 3 4 1 7 4 ...
$ :'data.frame': 58 obs. of 4 variables:
..$ x1: num [1:58] -0.9517 -0.1698 -0.048 0.2834 -0.0906 ...
..$ x2: num [1:58] -0.645 -0.336 -0.897 -2.072 1.005 ...
..$ RT: num [1:58] 0.471 0.598 0.224 0.486 1.053 ...
..$ LS: int [1:58] 6 2 1 4 5 3 4 1 4 2 ...

Can I sort Global Environment by type numeric and extract all into one CSV? Or create a data table in from 500+ values in each column directly in R?

So I've saved over 500 numeric values as dataframes in R. I now need to make a 3-column data table with these values.
Can I sort the values in the Global Environment as numeric and then extract them into one CSV?
Or perhaps it would be easier to just create the data table in R itself, however, I would rather avoid listing each variable's name out for the function: write.csv(rbind( ), file = "test.csv", row.names = T)
My data is as follows and I only need the numeric values, not the data tables.
> str(as.list(.GlobalEnv))
List of 1174
$ May26RMRA8comp :Classes ‘data.table’ and 'data.frame': 6 obs. of 6 variables:
..$ DateTime : chr [1:6] "2020/05/26 16:58:00" "2020/05/26 17:03:00" "2020/05/26 17:18:00" "2020/05/26 17:13:00" ...
..$ kcal_hr_M_8: num [1:6] 0.254 0.214 0.231 0.251 0.251 ...
..$ VO2_M_8 : num [1:6] 0.884 0.749 0.79 0.856 0.872 ...
..$ VCO2_M_8 : num [1:6] 0.695 0.583 0.706 0.766 0.711 ...
..$ Sum_6period: num [1:6] 1.46 1.5 1.52 1.53 1.54 ...
..$ ..I : int [1:6] 45 46 49 48 47 50
..- attr(*, ".internal.selfref")=<externalptr>
$ August24RMRA5comp:Classes ‘data.table’ and 'data.frame': 6 obs. of 6 variables:
..$ DateTime : chr [1:6] "2020/08/24 17:27:00" "2020/08/24 17:32:00" "2020/08/24 17:37:00" "2020/08/24 17:42:00" ...
..$ kcal_hr_M_5: num [1:6] 0.275 0.263 0.269 0.256 0.27 ...
..$ VO2_M_5 : num [1:6] 0.958 0.915 0.933 0.887 0.935 ...
..$ VCO2_M_5 : num [1:6] 0.774 0.734 0.76 0.721 0.765 ...
..$ Sum_6period: num [1:6] 1.61 1.61 1.67 1.77 1.83 ...
..$ ..I : int [1:6] 56 57 58 59 60 61
..- attr(*, ".internal.selfref")=<externalptr>
$ June11RMRA2comp :Classes ‘data.table’ and 'data.frame': 6 obs. of 6 variables:
..$ DateTime : chr [1:6] "2020/06/11 23:37:00" "2020/06/11 23:42:00" "2020/06/11 23:47:00" "2020/06/11 23:52:00" ...
..$ kcal_hr_M_2: num [1:6] 0.283 0.257 0.272 0.26 0.249 ...
..$ VO2_M_2 : num [1:6] 1.018 0.92 0.969 0.938 0.916 ...
..$ VCO2_M_2 : num [1:6] 0.675 0.613 0.675 0.646 0.633 ...
..$ Sum_6period: num [1:6] 1.59 1.6 1.7 1.73 1.76 ...
..$ ..I : int [1:6] 124 125 126 127 128 129
..- attr(*, ".internal.selfref")=<externalptr>
$ August12MassA1 : num 15.4
$ August12MassA2 : num 0
$ August12MassA3 : num 20.9
$ FinalRMJune7A1 : num 0.211
$ August12MassA4 : num 15.5
$ June1RMRA3comp :Classes ‘data.table’ and 'data.frame': 6 obs. of 6 variables:
..$ DateTime : chr [1:6] "2020/06/02 07:15:00" "2020/06/02 07:30:00" "2020/06/02 07:25:00" "2020/06/02 07:20:00" ...
..$ kcal_hr_M_3: num [1:6] 0.258 0.222 0.252 0.253 0.24 ...
..$ VO2_M_3 : num [1:6] 0.9 0.774 0.876 0.883 0.843 ...
..$ VCO2_M_3 : num [1:6] 0.703 0.609 0.713 0.694 0.639 ...
..$ Sum_6period: num [1:6] 1.49 1.49 1.5 1.5 1.51 ...
..$ ..I : int [1:6] 225 228 227 226 229 230
..- attr(*, ".internal.selfref")=<externalptr>
$ FinalRMJune7A2 : num 0.215
$ August12MassA5 : num 15.1
$ FinalRMJune7A3 : num 0.243
$ August12MassA6 : num 21.8
$ FinalRMJune7A4 : num 0.296
$ August12MassA7 : num 0
$ FinalRMJune7A5 : num 0.224
$ August12MassA8 : num 17.2
$ FinalRMJune7A6 : num 0.161
$ FinalRMJune7A7 : num 0.237
$ RMRVCO2May26A1 : num -0.00763
$ FinalRMJune7A8 : num 0.188
$ RMRVCO2May26A2 : num 0.699
$ RMRVCO2May26A3 : num 0.608
$ August19RMRA4comp:Classes ‘data.table’ and 'data.frame': 6 obs. of 6 variables:
..$ DateTime : chr [1:6] "2020/08/20 09:44:00" "2020/08/20 09:49:00" "2020/08/20 09:59:00" "2020/08/20 09:54:00" ...
..$ kcal_hr_M_4: num [1:6] 0.288 0.307 0.301 0.312 0.28 ...
..$ VO2_M_4 : num [1:6] 0.992 1.052 1.049 1.065 0.97 ...
..$ VCO2_M_4 : num [1:6] 0.827 0.9 0.862 0.889 0.804 ...
..$ Sum_6period: num [1:6] 1.76 1.79 1.8 1.8 1.8 ...
..$ ..I : int [1:6] 236 237 239 238 240 241
..- attr(*, ".internal.selfref")=<externalptr>
$ RMRVCO2May26A4 : num 0.59
$ RMRVCO2May26A5 : num 0.724
$ RMRVCO2May26A6 : num 0.71
$ RMRVCO2June1A1 : num 0.531
$ RMRVCO2May26A7 : num 0.702
$ July7RMRA7comp :Classes ‘data.table’ and 'data.frame': 6 obs. of 6 variables:
..$ DateTime : chr [1:6] "2020/07/08 08:28:00" "2020/07/08 08:33:00" "2020/07/08 08:43:00" "2020/07/08 08:38:00" ...
..$ kcal_hr_M_7: num [1:6] 0.275 0.189 0.178 0.239 0.141 ...
..$ VO2_M_7 : num [1:6] 0.928 0.64 0.605 0.813 0.489 ...
..$ VCO2_M_7 : num [1:6] 0.873 0.604 0.556 0.752 0.464 ...
..$ Sum_6period: num [1:6] 1.29 1.32 1.38 1.39 1.46 ...
..$ ..I : int [1:6] 218 219 221 220 222 223
..- attr(*, ".internal.selfref")=<externalptr>
$ RMRVCO2June1A2 : num 0.524
$ RMRVCO2May26A8 : num 0.701
$ RMRVCO2June1A3 : num 0.678
$ FinalRMJuly27A1 : num 0.258
$ RMRVCO2June1A4 : num 0.578
$ FinalRMJuly27A2 : num 0.219
$ RMRVCO2June1A5 : num 0.693
$ FinalRMJuly27A3 : num 0.204
$ RMRVCO2June1A6 : num 0.712
$ FinalRMJuly27A4 : num 0.185
$ RMRVCO2June1A7 : num 0.561
$ FinalRMJuly27A5 : num 0.321
$ RMRVCO2June1A8 : num 0.605
$ FinalRMJuly27A6 : num 0.27
$ RMRVCO2June16A1 : num 0.578
$ FinalRMJuly27A7 : num 0.202
$ RMRVCO2June16A2 : num 0.437
$ FinalRMJuly27A8 : num 0.229
$ RMRVCO2June16A3 : num 0.801
$ June7RMRA1 :Classes ‘data.table’ and 'data.frame': 286 obs. of 6 variables:
..$ DateTime : chr [1:286] "2020/07/07 10:05:00" "2020/07/07 10:50:00" "2020/07/07 10:55:00" "2020/07/07 12:00:00" ...
..$ kcal_hr_M_1: num [1:286] 0.216 0.227 0.198 0.257 0.232 ...
..$ VO2_M_1 : num [1:286] 0.746 0.776 0.679 0.869 0.792 ...
..$ VCO2_M_1 : num [1:286] 0.653 0.685 0.597 0.814 0.724 ...
..$ Sum_6period: num [1:286] 1.25 1.26 1.26 1.27 1.27 ...
..$ ..I : int [1:286] 254 263 264 277 262 253 269 265 266 268 ...
..- attr(*, ".internal.selfref")=<externalptr>
..- attr(*, "index")= int(0)
.. ..- attr(*, "__..I")= int [1:286] 174 168 164 153 150 156 158 148 136 146 ...
$ RMRVCO2June16A4 : num 0.726
$ June7RMRA2 :Classes ‘data.table’ and 'data.frame': 286 obs. of 6 variables:
..$ DateTime : chr [1:286] "2020/07/07 10:55:00" "2020/07/07 11:10:00" "2020/07/07 10:50:00" "2020/07/07 11:00:00" ...
..$ kcal_hr_M_2: num [1:286] 0.211 0.202 0.235 0.211 0.195 ...
..$ VO2_M_2 : num [1:286] 0.719 0.695 0.807 0.723 0.677 ...
..$ VCO2_M_2 : num [1:286] 0.638 0.611 0.702 0.628 0.589 ...
..$ Sum_6period: num [1:286] 1.27 1.28 1.29 1.29 1.29 ...
..$ ..I : int [1:286] 264 267 263 265 268 266 269 273 262 270 ...
..- attr(*, ".internal.selfref")=<externalptr>
..- attr(*, "index")= int(0)
.. ..- attr(*, "__..I")= int [1:286] 130 128 133 132 127 125 124 118 90 79 ...
$ RMRVCO2June16A5 : num 0.864
$ June7RMRA3 :Classes ‘data.table’ and 'data.frame': 286 obs. of 6 variables:
..$ DateTime : chr [1:286] "2020/07/07 12:05:00" "2020/07/07 12:15:00" "2020/07/07 12:10:00" "2020/07/07 11:25:00" ...
..$ kcal_hr_M_3: num [1:286] 0.237 0.24 0.245 0.247 0.263 ...
..$ VO2_M_3 : num [1:286] 0.826 0.837 0.86 0.859 0.917 ...
..$ VCO2_M_3 : num [1:286] 0.66 0.665 0.659 0.677 0.718 ...
..$ Sum_6period: num [1:286] 1.44 1.44 1.46 1.47 1.47 ...
..$ ..I : int [1:286] 278 280 279 270 269 277 268 271 276 273 ...
..- attr(*, ".internal.selfref")=<externalptr>
..- attr(*, "index")= int(0)
.. ..- attr(*, "__..I")= int [1:286] 160 167 186 207 218 217 206 185 149 129 ...
$ RMRVCO2June16A6 : num 0.569
$ June7RMRA4 :Classes ‘data.table’ and 'data.frame': 286 obs. of 6 variables:
..$ DateTime : chr [1:286] "2020/07/07 11:40:00" "2020/07/07 11:45:00" "2020/07/07 11:35:00" "2020/07/07 11:05:00" ...
..$ kcal_hr_M_4: num [1:286] 0.3 0.312 0.305 0.293 0.287 ...
..$ VO2_M_4 : num [1:286] 1.06 1.09 1.07 1.03 1.02 ...
..$ VCO2_M_4 : num [1:286] 0.812 0.845 0.835 0.785 0.813 ...
..$ Sum_6period: num [1:286] 1.76 1.77 1.78 1.79 1.79 ...
..$ ..I : int [1:286] 273 274 272 266 271 268 270 269 267 276 ...
..- attr(*, ".internal.selfref")=<externalptr>
..- attr(*, "index")= int(0)
.. ..- attr(*, "__..I")= int [1:286] 253 249 239 220 189 178 154 131 122 119 ...
$ RMRVCO2June16A7 : num 0.578
$ June7RMRA5 :Classes ‘data.table’ and 'data.frame': 286 obs. of 6 variables:
..$ DateTime : chr [1:286] "2020/07/07 10:50:00" "2020/07/07 10:55:00" "2020/07/07 10:45:00" "2020/07/07 11:00:00" ...
..$ kcal_hr_M_5: num [1:286] 0.234 0.231 0.232 0.216 0.201 ...
..$ VO2_M_5 : num [1:286] 0.796 0.786 0.783 0.726 0.675 ...
..$ VCO2_M_5 : num [1:286] 0.736 0.726 0.742 0.697 0.65 ...
..$ Sum_6period: num [1:286] 1.33 1.33 1.34 1.35 1.36 ...
..$ ..I : int [1:286] 263 264 262 265 266 261 280 267 278 260 ...
..- attr(*, ".internal.selfref")=<externalptr>
..- attr(*, "index")= int(0)
.. ..- attr(*, "__..I")= int [1:286] 189 167 139 132 119 102 79 72 68 43 ...
$ RMRVCO2June16A8 : num 0.678
$ June7RMRA6 :Classes ‘data.table’ and 'data.frame': 286 obs. of 6 variables:
..$ DateTime : chr [1:286] "2020/07/07 10:55:00" "2020/07/07 11:00:00" "2020/07/07 11:05:00" "2020/07/07 11:15:00" ...
..$ kcal_hr_M_6: num [1:286] 0.153 0.156 0.163 0.166 0.173 ...
..$ VO2_M_6 : num [1:286] 0.527 0.536 0.566 0.571 0.598 ...
..$ VCO2_M_6 : num [1:286] 0.438 0.436 0.457 0.48 0.494 ...
..$ Sum_6period: num [1:286] 0.963 0.975 0.979 0.982 0.985 ...
..$ ..I : int [1:286] 264 265 266 268 267 263 254 275 253 255 ...
..- attr(*, ".internal.selfref")=<externalptr>
..- attr(*, "index")= int(0)
.. ..- attr(*, "__..I")= int [1:286] 250 246 237 234 226 216 210 202 198 183 ...
$ June7RMRA7 :Classes ‘data.table’ and 'data.frame': 286 obs. of 6 variables:
..$ DateTime : chr [1:286] "2020/07/07 11:10:00" "2020/07/07 11:15:00" "2020/07/07 11:20:00" "2020/07/07 11:05:00" ...
..$ kcal_hr_M_7: num [1:286] 0.196 0.228 0.255 0.292 0.257 ...
..$ VO2_M_7 : num [1:286] 0.66 0.765 0.856 0.986 0.876 ...
..$ VCO2_M_7 : num [1:286] 0.625 0.742 0.831 0.933 0.807 ...
..$ Sum_6period: num [1:286] 1.41 1.44 1.5 1.5 1.51 ...
..$ ..I : int [1:286] 267 268 269 266 270 265 271 272 264 263 ...
..- attr(*, ".internal.selfref")=<externalptr>
..- attr(*, "index")= int(0)
.. ..- attr(*, "__..I")= int [1:286] 100 117 119 126 133 136 130 113 118 131 ...
$ June7RMRA8 :Classes ‘data.table’ and 'data.frame': 286 obs. of 6 variables:
..$ DateTime : chr [1:286] "2020/07/07 10:50:00" "2020/07/07 10:55:00" "2020/07/07 10:45:00" "2020/07/07 11:00:00" ...
..$ kcal_hr_M_8: num [1:286] 0.2 0.187 0.203 0.18 0.191 ...
..$ VO2_M_8 : num [1:286] 0.701 0.656 0.705 0.628 0.664 ...
..$ VCO2_M_8 : num [1:286] 0.516 0.494 0.532 0.5 0.529 ...
..$ Sum_6period: num [1:286] 1.12 1.12 1.14 1.14 1.15 ...
..$ ..I : int [1:286] 263 264 262 265 266 261 267 260 279 280 ...
..- attr(*, ".internal.selfref")=<externalptr>
..- attr(*, "index")= int(0)
.. ..- attr(*, "__..I")= int [1:286] 196 180 165 151 141 135 119 125 117 106 ...
$ June18RMRA8comp :Classes ‘data.table’ and 'data.frame': 6 obs. of 6 variables:
..$ DateTime : chr [1:6] "2020/06/19 10:57:00" "2020/06/19 11:02:00" "2020/06/19 11:07:00" "2020/06/19 11:12:00" ...
..$ kcal_hr_M_8: num [1:6] 0.152 0.16 0.16 0.169 0.177 ...
..$ VO2_M_8 : num [1:6] 0.524 0.553 0.552 0.58 0.606 ...
..$ VCO2_M_8 : num [1:6] 0.429 0.454 0.46 0.501 0.53 ...
..$ Sum_6period: num [1:6] 0.985 1.015 1.055 1.106 1.125 ...
..$ ..I : int [1:6] 256 257 258 259 260 261
..- attr(*, ".internal.selfref")=<externalptr>
$ August25RMRA6comp:Classes ‘data.table’ and 'data.frame': 6 obs. of 6 variables:
..$ DateTime : chr [1:6] "2020/08/26 08:33:00" "2020/08/26 08:38:00" "2020/08/26 08:43:00" "2020/08/26 08:48:00" ...
..$ kcal_hr_M_6: num [1:6] 0.257 0.259 0.25 0.253 0.261 ...
..$ VO2_M_6 : num [1:6] 0.943 0.954 0.922 0.933 0.952 ...
..$ VCO2_M_6 : num [1:6] 0.535 0.521 0.5 0.517 0.557 ...
..$ Sum_6period: num [1:6] 1.55 1.57 1.74 1.85 1.9 ...
..$ ..I : int [1:6] 226 227 228 229 230 231
..- attr(*, ".internal.selfref")=<externalptr>
$ June1DEEA1 : num 0.307
$ June1DEEA2 : num 0.356
$ June1DEEA3 : num 0.377
$ June1DEEA4 : num 0.37
$ June1DEEA5 : num 0.356
$ June1DEEA6 : num 0.407
$ June1DEEA7 : num 0.307
$ June1DEEA8 : num 0.387
$ August19MassA1 : num 24
$ July27MassA1 : num 18
$ August19MassA2 : num 20.8
$ July27MassA2 : num 0
$ August19MassA3 : num 15.7
$ July27MassA3 : num 18.2
$ August19MassA4 : num 21.1
$ July27MassA4 : num 0
$ August19MassA5 : num 0
$ July27MassA5 : num 26.3
$ August19MassA6 : num 18.4
$ July27MassA6 : num 16.1
$ August19MassA7 : num 17.6
$ July27MassA7 : num 18.3
$ August12RMRA1 :Classes ‘data.table’ and 'data.frame': 281 obs. of 6 variables:
..$ DateTime : chr [1:281] "2020/08/12 15:04:00" "2020/08/13 08:44:00" "2020/08/13 06:54:00" "2020/08/13 08:39:00" ...
..$ kcal_hr_M_1: num [1:281] 0.19 0.216 0.196 0.212 0.217 ...
..$ VO2_M_1 : num [1:281] 0.647 0.734 0.669 0.723 0.738 ...
..$ VCO2_M_1 : num [1:281] 0.557 0.674 0.595 0.645 0.66 ...
..$ Sum_6period: num [1:281] 1.19 1.22 1.23 1.23 1.23 ...
..$ ..I : int [1:281] 1 213 191 212 190 2 214 192 193 194 ...
..- attr(*, ".internal.selfref")=<externalptr>
..- attr(*, "index")= int(0)
.. ..- attr(*, "__..I")= int [1:281] 1 6 19 29 48 71 103 127 134 147 ...
$ August19MassA8 : num 19.8
$ July27MassA8 : num 16.1
$ August12RMRA2 :Classes ‘data.table’ and 'data.frame': 281 obs. of 6 variables:
..$ DateTime : chr [1:281] "2020/08/13 00:39:00" "2020/08/13 00:34:00" "2020/08/13 00:29:00" "2020/08/13 00:44:00" ...
..$ kcal_hr_M_2: num [1:281] 0.22 0.234 0.217 0.203 0.222 ...
..$ VO2_M_2 : num [1:281] 0.745 0.793 0.736 0.683 0.748 ...
..$ VCO2_M_2 : num [1:281] 0.697 0.736 0.666 0.651 0.648 ...
..$ Sum_6period: num [1:281] 1.22 1.25 1.27 1.27 1.28 ...
..$ ..I : int [1:281] 116 115 114 117 210 211 209 208 113 207 ...
..- attr(*, ".internal.selfref")=<externalptr>
..- attr(*, "index")= int(0)
.. ..- attr(*, "__..I")= int [1:281] 62 82 79 76 68 77 97 94 92 98 ...
$ August12RMRA3 :Classes ‘data.table’ and 'data.frame': 281 obs. of 6 variables:
..$ DateTime : chr [1:281] "2020/08/12 21:44:00" "2020/08/13 09:19:00" "2020/08/13 08:19:00" "2020/08/12 21:49:00" ...
..$ kcal_hr_M_3: num [1:281] 0.281 0.277 0.253 0.254 0.283 ...
..$ VO2_M_3 : num [1:281] 0.969 0.977 0.896 0.882 0.981 ...
..$ VCO2_M_3 : num [1:281] 0.809 0.741 0.655 0.72 0.796 ...
..$ Sum_6period: num [1:281] 1.52 1.52 1.52 1.53 1.53 ...
..$ ..I : int [1:281] 81 220 208 82 80 221 219 206 209 205 ...
..- attr(*, ".internal.selfref")=<externalptr>
..- attr(*, "index")= int(0)
.. ..- attr(*, "__..I")= int [1:281] 59 65 70 73 88 108 96 100 114 126 ...
$ August12RMRA4 :Classes ‘data.table’ and 'data.frame': 281 obs. of 6 variables:
..$ DateTime : chr [1:281] "2020/08/13 08:19:00" "2020/08/13 08:24:00" "2020/08/13 01:14:00" "2020/08/13 09:39:00" ...
..$ kcal_hr_M_4: num [1:281] 0.207 0.21 0.241 0.209 0.237 ...
..$ VO2_M_4 : num [1:281] 0.72 0.726 0.84 0.74 0.809 ...
..$ VCO2_M_4 : num [1:281] 0.558 0.567 0.67 0.637 0.699 ...
..$ Sum_6period: num [1:281] 1.36 1.38 1.38 1.39 1.39 ...
..$ ..I : int [1:281] 208 209 123 224 223 207 220 210 204 221 ...
..- attr(*, ".internal.selfref")=<externalptr>
..- attr(*, "index")= int(0)
.. ..- attr(*, "__..I")= int [1:281] 88 87 95 129 144 159 165 171 168 142 ...
$ June7RMRA1comp :Classes ‘data.table’ and 'data.frame': 6 obs. of 6 variables:
..$ DateTime : chr [1:6] "2020/07/07 10:05:00" "2020/07/07 10:10:00" "2020/07/07 10:15:00" "2020/07/07 10:30:00" ...
..$ kcal_hr_M_1: num [1:6] 0.216 0.194 0.204 0.211 0.218 ...
..$ VO2_M_1 : num [1:6] 0.746 0.663 0.696 0.716 0.741 ...
..$ VCO2_M_1 : num [1:6] 0.653 0.592 0.624 0.662 0.678 ...
..$ Sum_6period: num [1:6] 1.25 1.29 1.35 1.38 1.38 ...
..$ ..I : int [1:6] 254 255 256 259 257 258
..- attr(*, ".internal.selfref")=<externalptr>
$ August12RMRA5 :Classes ‘data.table’ and 'data.frame': 281 obs. of 6 variables:
..$ DateTime : chr [1:281] "2020/08/13 11:14:00" "2020/08/13 09:49:00" "2020/08/13 11:09:00" "2020/08/13 09:44:00" ...
..$ kcal_hr_M_5: num [1:281] 0.25 0.237 0.245 0.29 0.264 ...
..$ VO2_M_5 : num [1:281] 0.876 0.82 0.845 0.987 0.899 ...
..$ VCO2_M_5 : num [1:281] 0.699 0.665 0.713 0.791 0.754 ...
..$ Sum_6period: num [1:281] 1.42 1.42 1.44 1.46 1.46 ...
..$ ..I : int [1:281] 243 226 242 225 224 241 244 227 218 219 ...
..- attr(*, ".internal.selfref")=<externalptr>
..- attr(*, "index")= int(0)
.. ..- attr(*, "__..I")= int [1:281] 32 50 59 72 109 128 141 130 137 147 ...
$ August12RMRA6 :Classes ‘data.table’ and 'data.frame': 281 obs. of 6 variables:
..$ DateTime : chr [1:281] "2020/08/13 00:44:00" "2020/08/13 00:39:00" "2020/08/13 09:04:00" "2020/08/13 10:19:00" ...
..$ kcal_hr_M_6: num [1:281] 0.276 0.333 0.291 0.286 0.294 ...
..$ VO2_M_6 : num [1:281] 0.935 1.14 1.029 0.983 1.028 ...
..$ VCO2_M_6 : num [1:281] 0.856 1.028 0.811 0.805 0.797 ...
..$ Sum_6period: num [1:281] 1.71 1.72 1.73 1.73 1.73 ...
..$ ..I : int [1:281] 117 116 217 232 218 115 231 219 216 212 ...
..- attr(*, ".internal.selfref")=<externalptr>
..- attr(*, "index")= int(0)
.. ..- attr(*, "__..I")= int [1:281] 122 125 123 126 133 132 130 127 124 117 ...
$ August12RMRA7 :Classes ‘data.table’ and 'data.frame': 281 obs. of 6 variables:
..$ DateTime : chr [1:281] "2020/08/13 08:54:00" "2020/08/13 10:59:00" "2020/08/13 08:59:00" "2020/08/13 09:04:00" ...
..$ kcal_hr_M_7: num [1:281] 0.188 0.192 0.182 0.183 0.174 ...
..$ VO2_M_7 : num [1:281] 0.661 0.681 0.63 0.626 0.607 ...
..$ VCO2_M_7 : num [1:281] 0.516 0.573 0.516 0.521 0.532 ...
..$ Sum_6period: num [1:281] 1.13 1.14 1.15 1.15 1.15 ...
..$ ..I : int [1:281] 215 240 216 217 241 239 205 214 198 202 ...
..- attr(*, ".internal.selfref")=<externalptr>
..- attr(*, "index")= int(0)
.. ..- attr(*, "__..I")= int [1:281] 210 216 228 222 194 173 156 144 129 102 ...
$ August12RMRA8 :Classes ‘data.table’ and 'data.frame': 281 obs. of 6 variables:
..$ DateTime : chr [1:281] "2020/08/13 08:09:00" "2020/08/13 08:04:00" "2020/08/13 08:14:00" "2020/08/13 11:14:00" ...
..$ kcal_hr_M_8: num [1:281] 0.254 0.298 0.233 0.285 0.319 ...
..$ VO2_M_8 : num [1:281] 0.903 1.053 0.829 1.004 1.108 ...
..$ VCO2_M_8 : num [1:281] 0.641 0.761 0.59 0.743 0.889 ...
..$ Sum_6period: num [1:281] 1.48 1.52 1.52 1.53 1.55 ...
..$ ..I : int [1:281] 206 205 207 243 225 244 242 241 239 240 ...
..- attr(*, ".internal.selfref")=<externalptr>
..- attr(*, "index")= int(0)
.. ..- attr(*, "__..I")= int [1:281] 99 110 115 114 109 107 106 92 80 81 ...
$ June16RMRA5comp :Classes ‘data.table’ and 'data.frame': 6 obs. of 6 variables:
..$ DateTime : chr [1:6] "2020/06/17 07:37:00" "2020/06/17 07:42:00" "2020/06/17 07:47:00" "2020/06/17 07:52:00" ...
..$ kcal_hr_M_5: num [1:6] 0.3 0.285 0.301 0.327 0.319 ...
..$ VO2_M_5 : num [1:6] 1.039 0.991 1.041 1.137 1.119 ...
..$ VCO2_M_5 : num [1:6] 0.848 0.793 0.864 0.912 0.856 ...
..$ Sum_6period: num [1:6] 1.87 1.91 1.96 1.99 2 ...
..$ ..I : int [1:6] 220 221 222 223 224 225
..- attr(*, ".internal.selfref")=<externalptr>
$ RMRVO2June18A1 : num 1.03
[list output truncated]

Merge 2 list of lists in R [duplicate]

This question already has answers here:
Combining elements of list of lists by index
(3 answers)
in r combine a list of lists into one list
(3 answers)
Closed 4 years ago.
I have 2 list of lists in R with the same list name as follow :
str(total_delta_final[[1]])
List of 4
$ sector1_T02 :'data.frame': 24 obs. of 3 variables:
..$ DeltaF_1: num [1:24] 0.737 0.737 0.693 0.738 0.738 ...
..$ DeltaF_2: num [1:24] 0.24 0.24 0.279 0.239 0.239 ...
..$ DeltaF_3: num [1:24] 0.0233 0.0233 0.0275 0.0232 0.0232 ...
$ sector2_T03 :'data.frame': 24 obs. of 3 variables:
..$ DeltaF_1: num [1:24] 0.582 0.582 0.568 0.69 0.69 ...
..$ DeltaF_2: num [1:24] 0.377 0.377 0.39 0.282 0.282 ...
..$ DeltaF_3: num [1:24] 0.0406 0.0406 0.0426 0.0278 0.0278 ...
$ sector3_T03 :'data.frame': 24 obs. of 3 variables:
..$ DeltaF_1: num [1:24] 0.607 0.607 0.495 0.409 0.375 ...
..$ DeltaF_2: num [1:24] 0.356 0.356 0.451 0.519 0.544 ...
..$ DeltaF_3: num [1:24] 0.0373 0.0373 0.0541 0.072 0.0809 ...
$ sector12_T02:'data.frame': 24 obs. of 3 variables:
..$ DeltaF_1: num [1:24] 0.743 0.743 0.758 0.689 0.705 ...
..$ DeltaF_2: num [1:24] 0.234 0.234 0.22 0.283 0.269 ...
..$ DeltaF_3: num [1:24] 0.0226 0.0226 0.0213 0.028 0.0263 ...
> str(total_TI_final[[1]])
List of 4
$ sector1_T02 :'data.frame': 24 obs. of 3 variables:
..$ I_1: num [1:24] NA 0.0756 0.083 0.0799 0.0799 ...
..$ I_2: num [1:24] 0.122 NA 0.163 0.172 0.172 ...
..$ I_3: num [1:24] 0.212 0.211 NA 0.266 0.273 ...
$ sector2_T03 :'data.frame': 24 obs. of 3 variables:
..$ I_1: num [1:24] NA 0.0986 0.1013 0.1011 0.101 ...
..$ I_2: num [1:24] 0.15 NA 0.184 0.211 0.211 ...
..$ I_3: num [1:24] 0.249 0.249 NA 0.331 0.337 ...
$ sector3_T03 :'data.frame': 24 obs. of 3 variables:
..$ I_1: num [1:24] NA 0.119 0.115 0.113 0.105 ...
..$ I_2: num [1:24] 0.193 NA 0.2 0.193 0.177 ...
..$ I_3: num [1:24] 0.323 0.323 NA 0.277 0.256 ...
$ sector12_T02:'data.frame': 24 obs. of 3 variables:
..$ I_1: num [1:24] NA 0.0825 0.0681 0.0723 0.0706 ...
..$ I_2: num [1:24] 0.138 NA 0.146 0.145 0.144 ...
..$ I_3: num [1:24] 0.24 0.24 NA 0.22 0.226 ...
How could I merge these 2 list of lists in a way that my final output looks like total_TI_final[[1]][1] and the second list total_delta_final[[1]][1] then total_TI_final[[1]][2] and total_delta_final[[1]][2] and so on ...
We can use Map
Map(c, total_delta_final, total_TI_final)

Why does using c(data frame1 * data frame2) return a list when data frame1*data frame2 return a data frame

In R when I am multiplying two data frames within a c( ) function I get a list as an output but would like to have a data frame returned how can I do this.
Specifically to test it
str (c(Diesel_Col_Monthly_Prop_df[-1] * Product_Prop_Mch_df [ , 2]))
List of 12
$ Jan: num [1:14] 0.0335 0.0335 0.0335 0.0335 0.0335 ...
$ Feb: num [1:14] 0.0365 0.0365 0.0365 0.0365 0.0365 ...
$ Mar: num [1:14] 0.0581 0.0581 0.0581 0.0581 0.0581 ...
$ Apr: num [1:14] 0.0936 0.0936 0.0936 0.0936 0.0936 ...
$ May: num [1:14] 0.0783 0.0783 0.0783 0.0783 0.0783 ...
$ Jun: num [1:14] 0.0616 0.0616 0.0616 0.0616 0.0616 ...
$ Jul: num [1:14] 0.0713 0.0713 0.0713 0.0713 0.0713 ...
$ Aug: num [1:14] 0.107 0.107 0.107 0.107 0.107 ...
$ Sep: num [1:14] 0.0987 0.0987 0.0987 0.0987 0.0987 ...
$ Oct: num [1:14] 0.0654 0.0654 0.0654 0.0654 0.0654 ...
$ Nov: num [1:14] 0.034 0.034 0.034 0.034 0.034 ...
$ Dec: num [1:14] 0.0326 0.0326 0.0326 0.0326 0.0326 ...
> str (Diesel_Col_Monthly_Prop_df [-1] * Product_Prop_Mch_df [ ,2])
'data.frame': 14 obs. of 12 variables:
$ Jan: num 0.0335 0.0335 0.0335 0.0335 0.0335 ...
$ Feb: num 0.0365 0.0365 0.0365 0.0365 0.0365 ...
$ Mar: num 0.0581 0.0581 0.0581 0.0581 0.0581 ...
$ Apr: num 0.0936 0.0936 0.0936 0.0936 0.0936 ...
$ May: num 0.0783 0.0783 0.0783 0.0783 0.0783 ...
$ Jun: num 0.0616 0.0616 0.0616 0.0616 0.0616 ...
$ Jul: num 0.0713 0.0713 0.0713 0.0713 0.0713 ...
$ Aug: num 0.107 0.107 0.107 0.107 0.107 ...
$ Sep: num 0.0987 0.0987 0.0987 0.0987 0.0987 ...
$ Oct: num 0.0654 0.0654 0.0654 0.0654 0.0654 ...
$ Nov: num 0.034 0.034 0.034 0.034 0.034 ...
$ Dec: num 0.0326 0.0326 0.0326 0.0326 0.0326 ...
c(DF) where DF is a data frame always returns a list and DF * DF is a data frame so c(DF * DF) returns a list. If you want a data frame don't use c.
Using the builtin data frame BOD:
> str(BOD)
'data.frame': 6 obs. of 2 variables:
$ Time : num 1 2 3 4 5 7
$ demand: num 8.3 10.3 19 16 15.6 19.8
- attr(*, "reference")= chr "A1.4, p. 270"
> str(BOD * BOD)
'data.frame': 6 obs. of 2 variables:
$ Time : num 1 4 9 16 25 49
$ demand: num 68.9 106.1 361 256 243.4 ...
> str(c(BOD))
List of 2
$ Time : num [1:6] 1 2 3 4 5 7
$ demand: num [1:6] 8.3 10.3 19 16 15.6 19.8
> str(c(BOD*BOD))
List of 2
$ Time : num [1:6] 1 4 9 16 25 49
$ demand: num [1:6] 68.9 106.1 361 256 243.4 ...

Variogram Iteration Warning

I would like to perform kriging using a variable call "Secchi.Disk" from a data set I have. However, when fitting the variogram I get the following warming message
Warning message:
In fit.variogram(v, vgm(0.5, "Sph", 40, 0.5)) :
No convergence after 200 iterations: try different initial values?
I been trying different values for the partial sill, range and nugget; but, I still get the same warning message. Here is my code
library(rgdal)
library(ggplot2)
library(ggmap)
library(GISTools)
library(sp)
library(spdep)
library(gstat)
all.data<-read.csv("C:/Users/Juan/Desktop/All_Data_Fall_17.csv", header=TRUE)
str(all.data)
dput(all.data)
coordinates(all.data)<- ~ Lat + Lon
class(all.data)
#Variogram
#Secchi
v <- variogram(all.data$Secchi.Disk~1, all.data)
plot(v)
v.fit <- fit.variogram(v, vgm(0.5,"Sph",40,0.5))
Output of str(all.data)
Formal class 'SpatialPointsDataFrame' [package "sp"] with 5 slots
..# data :'data.frame': 12 obs. of 11 variables:
.. ..$ Sample.ID : Factor w/ 12 levels "CL-1","CL-10",..: 1 5 6 7 8 9 10 11 12 2 ...
.. ..$ Temp : num [1:12] 23 26.1 25.1 24.9 24.4 ...
.. ..$ TDS : num [1:12] 0.542 0.517 0.53 0.542 0.535 0.535 0.539 0.527 0.536 0.536 ...
.. ..$ pH : num [1:12] 8.08 8.74 8.67 8.51 8.69 8.7 8.56 8.75 8.63 8.6 ...
.. ..$ ODOsat : num [1:12] 60.9 216.1 156.7 112.6 114.8 ...
.. ..$ TSS : num [1:12] 68.3 68.8 67.5 65.4 56 ...
.. ..$ TP : num [1:12] 3.27 3.05 4.02 2.99 3.25 3.17 3.27 3.15 4.02 3.38 ...
.. ..$ TN : num [1:12] 13 12.4 12 12.4 15 12.6 12.4 12.4 12 13.6 ...
.. ..$ NO3.N : num [1:12] 2.71 3.02 2.71 1 2.58 1.35 1.66 1.88 4.07 4.16 ...
.. ..$ Chloro.a : num [1:12] 274 464 416 375 236 ...
.. ..$ Secchi.Disk: int [1:12] 18 15 16 15 15 15 17 16 16 17 ...
..# coords.nrs : int [1:2] 2 3
..# coords : num [1:12, 1:2] 334374 334381 334391 334423 334426 ...
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ : chr [1:12] "1" "2" "3" "4" ...
.. .. ..$ : chr [1:2] "Lat" "Lon"
..# bbox : num [1:2, 1:2] 334374 4088924 334489 4089041
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ : chr [1:2] "Lat" "Lon"
.. .. ..$ : chr [1:2] "min" "max"
..# proj4string:Formal class 'CRS' [package "sp"] with 1 slot
.. .. ..# projargs: chr NA
Any ideas on how to fix this problem?
Thank you

Resources