Reputation: 1
I have a list(RS_maja) that includes 28 sub-lists as you can see the 28th. I need to reach the third column of every list. It is SENTINEL2B_20210627.153206.359_L2A_T18NUK_C_V1.0_CLM_R1
in this case.
I will apply a function to 28 list object's 3rd column using lapply
. How can I index to the 3rd column?
RS_maja
.
.
.
[[28]]
x y SENTINEL2B_20210627.153206.359_L2A_T18NUK_C_V1.0_CLM_R1
[1,] 347075 482915 0
[2,] 330675 453715 0
[3,] 362095 420345 15
[4,] 335225 398995 0
[5,] 403675 414815 43
[6,] 372505 476495 11
[7,] 326175 419255 0
The function will use the third column as an input and will convert it to a label as; cloud, shadow, cirrus etc.
if I take not entire list but 4th element of the RS_maja;
RS_maja_4 = RS_maja[[4]] #this is the 4th element
y = RS_maja_4[,3] #this is the 4th element's 3rd column.
maja_recode = recode_maja(y) #applying function
the output should be this;
[1] "clear" "clear" "clear" "clear" "cirrus" "clear" "cloud" "clear" "cloud" "cloud"
In case I'm defining the data type incorrectly;
> class(RS_maja)
[1] "list"
> class(RS_maja[1])
[1] "list"
>class(RS_maja[[1]])
[1] "matrix" "array"
To be more clear about data;
dput(RS_maja[1:2])
list(structure(c(330705, 320285, 320755, 384575, 301815, 390405,
387755, 372615, 407025, 372185, 336095, 323595, 359375, 308295,
325545, 386115, 334005, 341105, 344615, 358975, 328335, 344205,
386655, 400125, 367975, 341625, 363715, 304255, 339365, 338405,
407135, 381845, 394775, 332125, 334235, 348075, 352955, 393625,
348885, 402705, 378825, 370295, 370385, 345945, 336025, 378665,
380875, 381755, 409175, 341645, 310925, 372675, 357915, 351285,
380755, 351345, 352405, 400375, 317765, 383275, 343295, 394445,
303715, 352375, 387155, 354515, 356545, 386665, 309345, 303015,
342545, 408835, 352555, 358155, 407895, 343965, 335115, 403605,
348125, 396395, 380895, 349965, 346605, 345825, 382005, 330215,
399175, 371955, 392965, 340835, 340135, 326925, 345685, 339815,
354965, 377925, 390145, 403265, 336135, 379995, 434965, 445435,
466335, 440165, 465555, 476965, 446665, 407225, 429185, 396565,
438755, 495025, 427235, 417295, 464685, 478125, 400855, 434875,
495415, 477365, 439805, 469125, 472305, 422625, 437605, 454675,
490715, 483055, 476555, 423585, 465115, 499865, 499945, 492705,
397205, 414065, 437735, 420595, 395905, 461075, 452585, 477075,
423365, 462305, 402925, 469195, 451155, 443765, 410005, 443475,
424815, 475405, 390275, 397575, 484415, 454705, 491955, 482515,
497075, 465325, 408005, 450005, 493705, 496565, 412305, 402155,
426195, 440225, 393985, 408585, 406905, 499415, 481195, 465315,
485315, 397085, 403925, 444215, 462085, 466595, 415205, 459265,
429075, 413315, 496925, 424875, 396905, 464645, 453035, 430675,
403635, 459305, 478405, 456795, 407105, 449675, 444265, 442225,
462605, 482265, 7, 135, 135, 0, 135, 11, 0, 7, 0, 0, 135, 135,
135, 135, 135, 143, 135, 135, 135, 7, 7, 135, 143, 0, 135, 135,
7, 135, 135, 7, 0, 143, 159, 135, 135, 0, 135, 0, 135, 0, 135,
143, 7, 135, 0, 143, 135, 0, 0, 135, 135, 143, 135, 135, 143,
135, 135, 0, 131, 139, 7, 0, 135, 135, 0, 135, 135, 0, 0, 135,
0, 0, 135, 135, 0, 131, 0, 0, 135, 0, 0, 135, 135, 0, 143, 0,
0, 143, 0, 135, 0, 135, 135, 135, 135, 135, 0, 0, 135, 143), .Dim = c(100L,
3L), .Dimnames = list(NULL, c("x", "y", "SENTINEL2B_20210210.154159.565_L2A_T18NUK_C_V1.0_CLM_R1"
))), structure(c(359325, 381165, 401365, 391895, 327755, 373585,
378995, 398995, 406355, 323235, 362275, 384855, 387285, 347885,
360645, 399995, 333365, 353135, 393875, 387535, 326115, 373335,
306075, 310365, 336195, 370455, 395805, 334215, 358015, 334675,
342595, 385315, 376455, 403275, 346235, 335065, 336805, 317345,
360725, 367585, 408485, 346575, 348155, 346095, 406225, 371015,
330335, 395595, 377255, 374375, 348135, 357585, 300155, 394355,
311835, 407725, 312145, 385535, 348695, 365905, 382225, 367815,
319815, 314315, 304715, 343995, 340755, 376495, 321115, 327335,
378645, 393075, 336965, 344825, 328465, 359545, 359675, 367705,
321105, 393505, 375855, 319285, 314915, 338395, 347065, 393355,
384005, 344045, 400425, 348935, 392815, 350105, 374365, 377395,
330165, 398365, 337275, 317405, 331735, 367695, 397205, 497905,
444105, 440545, 395215, 405035, 445815, 406665, 412875, 478945,
471755, 401945, 487995, 464875, 421825, 475425, 476245, 424155,
461915, 494215, 416425, 461245, 390745, 422985, 398595, 415725,
480145, 440785, 423345, 484985, 392825, 399825, 474345, 448545,
453265, 393075, 461325, 477845, 415295, 410445, 399195, 444335,
448555, 452195, 490695, 475525, 426865, 445635, 411415, 391625,
435195, 428235, 434675, 492865, 426965, 478475, 491075, 440845,
464315, 458805, 445535, 475955, 492995, 473395, 438965, 417275,
451835, 447655, 459985, 462765, 462155, 406555, 479145, 491835,
435655, 485975, 438485, 454545, 455895, 432045, 449835, 445365,
453805, 404835, 412035, 412635, 496375, 475835, 495545, 485925,
436525, 426925, 493045, 444765, 494965, 401345, 420305, 475835,
421935, 460305, 131, 155, 7, 7, 0, 7, 0, 0, 7, 0, 0, 7, 143,
0, 135, 7, 0, 0, 7, 159, 0, 143, 0, 0, 0, 135, 27, 0, 0, 0, 0,
7, 143, 0, 0, 0, 0, 0, 131, 7, 7, 0, 0, 0, 7, 143, 0, 7, 7, 7,
0, 0, 0, 0, 0, 15, 0, 7, 0, 0, 7, 0, 0, 0, 0, 0, 0, 135, 0, 0,
143, 7, 0, 0, 0, 0, 0, 131, 0, 7, 135, 0, 0, 0, 0, 7, 143, 0,
31, 0, 7, 0, 131, 7, 0, 0, 0, 0, 0, 155), .Dim = c(100L, 3L), .Dimnames = list(
NULL, c("x", "y", "SENTINEL2B_20210217.153203.301_L2A_T18NUK_C_V1.0_CLM_R1"
))))
Upvotes: 0
Views: 126
Reputation: 9858
It looks like you have a list of data.frames
You can use purrr::map
An example with a custom function and a toy list:
#Create toy list
a<-list(one=data.frame(a=1:2, b=2:3, c=3:4), two=data.frame(a=1:2, b=2:3, c=3:4))
#create custom function
my_function<-function(x) {x*2}
Apply custom function in a loop to every third list element
library(purrr)
map(a, 3) %>% map(my_function)
#OR
map(a, ~ my_function(.x[3]))
If you need to do it in base R, you can use:
lapply(a, function(x) my_function(x[[3]])
Output
$one
[1] 6 8
$two
[1] 6 8
Upvotes: 1