问题
I have a folder with 10 files that I import in R as S
S<-list.files(S1_path, recursive = TRUE, full.names = TRUE, pattern="S1")
> S
[1] "/shared/Training/EARSEL0918_UrbanClassification_Germany/Original//S1A_IW_SLC__1SDV_20180412T171648_20180412T171715_021437_024E95_BDA1.zip"
[2] "/shared/Training/EARSEL0918_UrbanClassification_Germany/Original//S1A_IW_SLC__1SDV_20180424T171648_20180424T171715_021612_02540A_BB21.zip"
[3] "/shared/Training/EARSEL0918_UrbanClassification_Germany/Original//S1A_IW_SLC__1SDV_20180506T171649_20180506T171716_021787_025996_98AB.zip"
[4] "/shared/Training/EARSEL0918_UrbanClassification_Germany/Original//S1A_IW_SLC__1SDV_20180518T171649_20180518T171716_021962_025F27_A15C.zip"
[5] "/shared/Training/EARSEL0918_UrbanClassification_Germany/Original//S1A_IW_SLC__1SDV_20180530T171650_20180530T171717_022137_0264C8_5D94.zip"
[6] "/shared/Training/EARSEL0918_UrbanClassification_Germany/Original//S1A_IW_SLC__1SDV_20180611T171651_20180611T171718_022312_026A3D_BBFC.zip"
[7] "/shared/Training/EARSEL0918_UrbanClassification_Germany/Original//S1A_IW_SLC__1SDV_20180623T171652_20180623T171719_022487_026F7C_450E.zip"
[8] "/shared/Training/EARSEL0918_UrbanClassification_Germany/Original//S1A_IW_SLC__1SDV_20180705T171652_20180705T171719_022662_027499_1B8F.zip"
[9] "/shared/Training/EARSEL0918_UrbanClassification_Germany/Original//S1A_IW_SLC__1SDV_20180717T171653_20180717T171720_022837_0279EC_5E5E.zip"
[10] "/shared/Training/EARSEL0918_UrbanClassification_Germany/Original//S1A_IW_SLC__1SDV_20180729T171654_20180729T171721_023012_027F72_97F6.zip"
Using S as input, I want to create two new differnet lists. One containing the files in positions [1], [3],[5],[7],[9] and the other containing files in positions [2], [4],[6],[8],[10].
I am trying to adapt a previous code of mine. Here I create an empty list that I fill iterating over S but 1 by 1 and not in the way I want.
input<-list()
for (i in S){
input[[i]]<-paste("-Pinput1=", i, sep="")
}
Do you have any suggestion. Something like i+2 ?
回答1:
Would this come close to what you need?
library(tidyverse)
list1 <- S[seq(1, 9, 2)] %>%
map(~paste0("-Pinput1=", .x))
The first line is extracting the odd positions from your list and the second one is pasting the string -Pinput1= in front of each
回答2:
Try this example:
S <- c("a", "b", "c", "d")
S_odd <- paste0("-Pinput1=", S[ c(TRUE, FALSE) ])
S_even <- paste0("-Pinput1=", S[ c(FALSE, TRUE) ])
S_odd
# [1] "-Pinput1=a" "-Pinput1=c"
S_even
# [1] "-Pinput1=b" "-Pinput1=d"
回答3:
Easiest way:
even_indexes<-seq(2,10,2) # List of even indexes
odd_indexes<-seq(1,10,2) # List of odd indexes
paste0("-Pinput1=",df[odd_indexes,]) # Name with odd index
paste0("-Pinput1=",df[even_indexes,]) # Name with even index
回答4:
Adding all your ideas I managed to find a solution too:
input<-list()
for (i in S[seq(1, 10, 2)]){
input[[i]]<-paste("-Pinput1=", i, sep="")
}
and
input<-list()
for (i in S[seq(2, 10, 2)]){
input[[i]]<-paste("-Pinput1=", i, sep="")
}
来源:https://stackoverflow.com/questions/52218824/create-list-in-r-with-specific-iteration