Remove items from a list in R

2

I have a list that is made up of 1000 data.frame (replicas), where each of the data.frame consists of 16 rows and 22 columns

And I want to create lists where: List 1: this is composed of the values of column 1 and row 1 of all the replicas. List two: this consists of the values of column 1 and row 2 Lists three: columns 1 row 3 So on with all rows and columns

If I do the following mdresult[[1]]$cork_est[[1]] , where mdresul refers to the list where 1000 data.frame and cork_est are to the name of the first column. The result I get is from replica 1 the first value (row 1) of the column cork_est that refers to the first column.

    
asked by Delia Arroyo Resino 28.11.2017 в 18:07
source

2 answers

2

Delia, the idea of creating a minimum, complete and verifiable example as Mpaladino tells you is essential for those who we are on the other side, on the one hand to understand the problem and on the other it saves us many difficulties. Luckily, in R , playing your example is very simple. You mention a list of 1000 items, where each one is a data.frame of 22 columns and 16 rows, something like this:

# Creamos un dataframe como el ejemplo
df <- data.frame(matrix(1:(22*16), nrow=16))
df <- df[rep(seq_len(nrow(df)),1000),]
mdresult <- split(df,rep(1:ceiling(nrow(df)/16),each=16)[1:nrow(df)])

If we see the first data.frame of the list with mdresult[[1]] :

    X1 X2 X3 X4 X5 X6 X7  X8  X9 X10 X11 X12 X13 X14 X15 X16 X17 X18 X19 X20 X21 X22
1   1 17 33 49 65 81  97 113 129 145 161 177 193 209 225 241 257 273 289 305 321 337
2   2 18 34 50 66 82  98 114 130 146 162 178 194 210 226 242 258 274 290 306 322 338
3   3 19 35 51 67 83  99 115 131 147 163 179 195 211 227 243 259 275 291 307 323 339
4   4 20 36 52 68 84 100 116 132 148 164 180 196 212 228 244 260 276 292 308 324 340
5   5 21 37 53 69 85 101 117 133 149 165 181 197 213 229 245 261 277 293 309 325 341
6   6 22 38 54 70 86 102 118 134 150 166 182 198 214 230 246 262 278 294 310 326 342
7   7 23 39 55 71 87 103 119 135 151 167 183 199 215 231 247 263 279 295 311 327 343
8   8 24 40 56 72 88 104 120 136 152 168 184 200 216 232 248 264 280 296 312 328 344
9   9 25 41 57 73 89 105 121 137 153 169 185 201 217 233 249 265 281 297 313 329 345
10 10 26 42 58 74 90 106 122 138 154 170 186 202 218 234 250 266 282 298 314 330 346
11 11 27 43 59 75 91 107 123 139 155 171 187 203 219 235 251 267 283 299 315 331 347
12 12 28 44 60 76 92 108 124 140 156 172 188 204 220 236 252 268 284 300 316 332 348
13 13 29 45 61 77 93 109 125 141 157 173 189 205 221 237 253 269 285 301 317 333 349
14 14 30 46 62 78 94 110 126 142 158 174 190 206 222 238 254 270 286 302 318 334 350
15 15 31 47 63 79 95 111 127 143 159 175 191 207 223 239 255 271 287 303 319 335 351
16 16 32 48 64 80 96 112 128 144 160 176 192 208 224 240 256 272 288 304 320 336 352

The remaining 999 are exactly the same. Now focusing on your question:

  • List 1: this consists of the values of column 1 and row 1 of all the replicas.
  • List two: this consists of the values of column 1 and row 2
  • Lists three: columns 1 row 3
  • etc.

One possible way is this:

lista.nueva <- split(as.vector(unlist(lapply(mdresult, '[[', 1))), rep(1:16, 1000))
  • With as.vector(unlist(lapply(mdresult, '[[', 1))) we do: (a) We cut from all data.frame of the list, the first column, (b) we convert everything to a single vector , we end up with something like this: <col1-fila1><col1-fila2>..<col1-fila1000>
  • Finally, with rep(1:16, 1000) we generate a "mask" for clipping by split() , which ends up leaving us with a list of 1000 vectors, each with the full column 1 of each row of the original list.

If we see, for example, the first element of the initial list, we can see that it is composed of an element from column 1 of each of the replicas

lista.nueva[[1]]
 [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
  [44] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
  [87] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
 [130] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 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 1 1
 [646] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
 [689] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
 [732] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
 [775] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
 [818] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
 [861] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
 [904] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
 [947] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
 [990] 1 1 1 1 1 1 1 1 1 1 1
    
answered by 29.11.2017 в 03:05
1

Patricio's solution is very elegant and as usual does not depend on any bookstore.

I tried to find a simpler one using dplyr and purrr, but I do not think it has improved in that sense: it continues to generate very complex intermediate data structures. Making subsets of lists is complicated.

The attempt goes anyway:

library(tidyverse)       #Para transpose(), map() y as_tibble()

mdresult %>%             #La lista que creó Patricio.
  transpose() %>%        #Transpongo la lista, ahora se organiza por las columnas de los df en su interior. 22 elementos en el primer nivel. 
  .$X1 %>%               #Extraigo la columna 1, por nombre. .$cork_est en la pregunta.
  as_tibble() %>%        #Lo paso a un data.frame de 16*1000, en el que cada fila contiene las 1000 replicaciones correspondientes a la fila con el mismo índice de los data.frame que están dentro de mdresult 
  split(1:nrow(.)) %>%   #A lista de nuevo.
  map(unlist) %>%        #Simplifico cada elemento de la lista, si no lista dentro de lista.
  map(as.vector)         #Lo paso a vector, elimina los nombres de heredados de las columna X1:X1000 de que creó as_tibble() 

What are you doing?

The key function is purrr::transpose() , which restructures the list so that the internal elements (in this case, the columns of the data.frame within mdresult) are converted into external elements.

In this way I achieve two things: I leave the data ready to make the subset by selecting only the first column of the "internal" data.frame of mdresult with .$X1 . Additionally the transposed list already has the order that I require, the rows of the internal data.frame of mdresult are "aligned" (virtually).

With as_tibble() this is no longer virtual: this function rearranges the elements of the list in columns (*). I use as_tibble just because I find it easier to see the output on the screen, as.data.frame() does the same. The rest is to simplify and delete the names, to bring to a list of 16 long 1000 vectors.

Greetings,

Martín

*. The data.frame are also ready!

    
answered by 29.11.2017 в 16:06