I want to repeat the same operations in multiple files that have the same format [1:1259]. Each file has a column name Image in which I want to extract a number and create another column with it.
The code I want to repeat to each of my files.
r<- regexpr("\\d+", Seg_grow_1mm.csv[,"Image"])
Seg_grow_1mm_01<- Seg_grow_1mm.csv %>%
mutate(., new_id=(regmatches(Seg_grow_1mm.csv[,"Image"], r)))
Preview of Seg_grow_1mm_01
# ID Image New_id
# 02 /Users/LLG/Data avec smoothing-margin/CHUM/05/Augmentation 3 mm/ 05
# 03 /Users/LLG/Data avec smoothing-margin/CHUM/103/Augmentation 3 mm/ 103
# 04 /Users/LLG/Data avec smoothing-margin/CHUM/145/Augmentation 3 mm/ 145
# ....
I want to repeat this operation to each of my files. I tried with a loop without success and I don’t know how to transform it into a function so I can use lapply on my list of files.
seg = list.files(path=csv, pattern="*.csv") # Seg[1:3]
for (i in 1:length(seg))
assign(seg[i], read.csv(seg[i]))for (x in seg)
r<- regexpr("\\d+", x[,"Image"])
mutate(., new_id=(regmatches( x[,"Image"], r)))
Error in x[, "Image"] : incorrect number of dimensions
I don't know what to put at the ??.
seg01<- lapply(seg, function (z)
{r<- regexpr("\\d+", ?? [,"Image"])
mutate(., new_id=(regmatches( ?? [,"Image"], r)))})
Thank you for the help!