Modérateur : Groupe des modérateurs
Code : Tout sélectionner
ecoutes <- do.call("rbind", lapply(formatC(1:3, width =3 , flag = "0"), lire_point_ecoute))
Code : Tout sélectionner
ecoutes <- do.call("rbind", lapply(formatC(1:142, width =3 , flag = "0"), lire_point_ecoute))
Code : Tout sélectionner
if (file.exists(...)) { ... }
Code : Tout sélectionner
sprintf("%03d", 1:142) # = formatC(1:142, width = 3 , flag = "0")
Code : Tout sélectionner
ifelse (file.exists("*.csv")== TRUE, ecoutes <- do.call("rbind", lapply(formatC(1:142, width =3 , flag = "0"), lire_point_ecoute)),print("Pas de csv"))
[1] "Pas de csv"
[1] "Pas de csv"
ifelse (file.exists("res_5_", x, "__SDOA_nuit_.*.csv")== TRUE, ecoutes <- do.call("rbind", lapply(formatC(1:142, width =3 , flag = "0"), lire_point_ecoute)),print("Pas de csv"))
Error in file.exists("res_5_", x, "__SDOA_nuit_.*.csv") :
objet 'x' introuvable
if (file.exists(".csv")== TRUE) {ecoutes <- do.call("rbind", lapply(sprintf("%03d", 1:142), lire_point_ecoute))}
Code : Tout sélectionner
?file.exists
Code : Tout sélectionner
lire_point_ecoute <- function(x, path = ".") {
fname <- list.files(
path = path,
pattern = paste0("res_15", x, "SDOA_nuit_.*.csv"),
full.names = TRUE
)
ans <- read.csv(fname)
ans$point_id <- x
ans[c("point_id", setdiff(names(ans), "point_id"))]
}
ecoutes <- do.call("rbind", lapply(formatC(1:3, width = 3, flag = "0"), lire_point_ecoute))
Code : Tout sélectionner
unlink(file.path(tempdir(), "SDOA_nuit"), recursive = TRUE)
dir.create(file.path(tempdir(), "SDOA_nuit"))
for (i in 1:5) {
write.csv2(
x = sleep,
file = tempfile(
pattern = paste0("res_15", sprintf("%03d", i), "SDOA_nuit_"),
tmpdir = file.path(tempdir(), "SDOA_nuit"),
fileext = ".csv"
)
)
}
cat("", file = file.path(tempdir(), "SDOA_nuit", "some_random_file.csv"))
list.files(path = file.path(tempdir(), "SDOA_nuit"))
#> [1] "res_15001SDOA_nuit_3eb42c4a12.csv" "res_15002SDOA_nuit_3eb46944359a.csv"
#> [3] "res_15003SDOA_nuit_3eb41c5240f3.csv" "res_15004SDOA_nuit_3eb43f6a739f.csv"
#> [5] "res_15005SDOA_nuit_3eb45dfa7d29.csv" "some_random_file.csv"
Code : Tout sélectionner
x <- 1 # parameter
path <- file.path(tempdir(), "SDOA_nuit") # parameter
fname <- list.files(# list full path of files in "path" according to the "pattern"
path = path,
pattern = paste0("res_15", formatC(x, width = 3, flag = "0"), "SDOA_nuit_.*.csv"),
full.names = TRUE
)
fname
#> [1] "D:\\Profils\\mcanouil\\AppData\\Local\\Temp\\RtmpK2F8ir/SDOA_nuit/res_15001SDOA_nuit_3eb42c4a12.csv"
ans <- read.csv2(fname) # import a french csv as a data.frame
ans$point_id <- x # add a column "point_id" with the value in "x"
ans[c("point_id", setdiff(names(ans), "point_id"))] # reorder column to have "point_id" as first column
#> point_id X extra group ID
#> 1 1 1 0.7 1 1
#> 2 1 2 -1.6 1 2
#> 3 1 3 -0.2 1 3
#> 4 1 4 -1.2 1 4
#> 5 1 5 -0.1 1 5
Code : Tout sélectionner
lire_point_ecoute <- function(x, path) {
fname <- list.files(
path = path,
pattern = paste0("res_15", formatC(x, width = 3, flag = "0"), "SDOA_nuit_.*.csv"),
full.names = TRUE
)
ans <- read.csv2(fname)
ans$point_id <- formatC(x, width = 3, flag = "0")
ans[c("point_id", setdiff(names(ans), "point_id"))]
}
lire_point_ecoute(x = 1, path = file.path(tempdir(), "SDOA_nuit"))
#> point_id X extra group ID
#> 1 001 1 0.7 1 1
#> 2 001 2 -1.6 1 2
#> 3 001 3 -0.2 1 3
#> 4 001 4 -1.2 1 4
#> 5 001 5 -0.1 1 5
list_df_from_csv <- lapply( # read all files using the integer id
X = 1:5,
FUN = lire_point_ecoute, path = file.path(tempdir(), "SDOA_nuit")
)
str(list_df_from_csv, 1)
#> List of 5
#> $ :'data.frame': 20 obs. of 5 variables:
#> $ :'data.frame': 20 obs. of 5 variables:
#> $ :'data.frame': 20 obs. of 5 variables:
#> $ :'data.frame': 20 obs. of 5 variables:
#> $ :'data.frame': 20 obs. of 5 variables:
ecoutes <- do.call("rbind", list_df_from_csv)
str(ecoutes)
#> 'data.frame': 100 obs. of 5 variables:
#> $ point_id: chr "001" "001" "001" "001" ...
#> $ X : int 1 2 3 4 5 6 7 8 9 10 ...
#> $ extra : num 0.7 -1.6 -0.2 -1.2 -0.1 3.4 3.7 0.8 0 2 ...
#> $ group : int 1 1 1 1 1 1 1 1 1 1 ...
#> $ ID : int 1 2 3 4 5 6 7 8 9 10 ...
Code : Tout sélectionner
file_to_delete <- list.files(
path = file.path(tempdir(), "SDOA_nuit"),
pattern = paste0("res_15", formatC(1, width = 3, flag = "0"), "SDOA_nuit_.*.csv"),
full.names = TRUE
)
file_to_delete
#> [1] "D:\\Profils\\mcanouil\\AppData\\Local\\Temp\\RtmpK2F8ir/SDOA_nuit/res_15001SDOA_nuit_3eb42c4a12.csv"
file.exists(file_to_delete) # if if file exist
#> [1] TRUE
unlink( # delete file "1" to reproduce the issue at hand
x = list.files(
path = file.path(tempdir(), "SDOA_nuit"),
pattern = paste0("res_15", formatC(1, width = 3, flag = "0"), "SDOA_nuit_.*.csv"),
full.names = TRUE
)
)
Code : Tout sélectionner
list_df_from_csv <- lapply( # read all files using the integer id
X = 1:5,
FUN = lire_point_ecoute, path = file.path(tempdir(), "SDOA_nuit")
)
#> Error in file(file, "rt"): invalid 'description' argument
lire_point_ecoute(x = 1, path = file.path(tempdir(), "SDOA_nuit"))
#> Error in file(file, "rt"): invalid 'description' argument
Code : Tout sélectionner
x <- 1 # parameter
path <- file.path(tempdir(), "SDOA_nuit") # parameter
fname <- list.files(# list full path of files in "path" according to the "pattern"
path = path,
pattern = paste0("res_15", formatC(x, width = 3, flag = "0"), "SDOA_nuit_.*.csv"),
full.names = TRUE
)
fname # nothing
#> character(0)
ans <- read.csv2(fname) # error because R can't read nothing
#> Error in file(file, "rt"): invalid 'description' argument
Code : Tout sélectionner
# Because the pattern uses regular expression ".*", file.exists can't be used here
# But we can check if fname contains something, i.e., a path, before doing anything else "nothing"
if (length(fname) != 0) {
ans <- read.csv2(fname)
}
Code : Tout sélectionner
lire_point_ecoute <- function(x, path) {
fname <- list.files(
path = path,
pattern = paste0("res_15", formatC(x, width = 3, flag = "0"), "SDOA_nuit_.*.csv"),
full.names = TRUE
)
if (length(fname) != 0) {
ans <- read.csv2(fname)
ans$point_id <- formatC(x, width = 3, flag = "0")
ans[c("point_id", setdiff(names(ans), "point_id"))]
}
}
Code : Tout sélectionner
lire_point_ecoute(x = 1, path = file.path(tempdir(), "SDOA_nuit"))
Code : Tout sélectionner
list_df_from_csv <- lapply( # read all files using the integer id
X = 1:5,
FUN = lire_point_ecoute, path = file.path(tempdir(), "SDOA_nuit")
)
str(list_df_from_csv, 1)
#> List of 5
#> $ : NULL
#> $ :'data.frame': 20 obs. of 5 variables:
#> $ :'data.frame': 20 obs. of 5 variables:
#> $ :'data.frame': 20 obs. of 5 variables:
#> $ :'data.frame': 20 obs. of 5 variables:
ecoutes <- do.call("rbind", list_df_from_csv)
str(ecoutes)
#> 'data.frame': 80 obs. of 5 variables:
#> $ point_id: chr "002" "002" "002" "002" ...
#> $ X : int 1 2 3 4 5 6 7 8 9 10 ...
#> $ extra : num 0.7 -1.6 -0.2 -1.2 -0.1 3.4 3.7 0.8 0 2 ...
#> $ group : int 1 1 1 1 1 1 1 1 1 1 ...
#> $ ID : int 1 2 3 4 5 6 7 8 9 10 ...
Loic SALAUN 2 a écrit :Do.call j’avoue avoir plus de mal à saisir le sens exact. Si j’ai bien compris cela applique Rbind (donc ajout de ligne) à ma liste. Mais en effet cela me paraît un pas très limpide…
Code : Tout sélectionner
rbind(df1, df2, df3)
Code : Tout sélectionner
do.call(rbind, list(df1, df2, df3))
Retourner vers « Questions en cours »
Utilisateurs parcourant ce forum : Aucun utilisateur enregistré et 1 invité