I come up with a problem that I have not been able to solve working with R. I appreciate the support of the community. Attached an example of my data as shown in my .csv files. I need to create columns (factors) with part of the information specified in the first column (Image name).
So, I need to go from this:
Image | Class | Num Detections |
---|---|---|
KLF4_Exp1_M012_KO25_MCAO_S1.tif | Left: fiber tracts | 206 |
KLF4_Exp1_M012_KO25_MCAO_S10.tif | left: fiber tracts | 1632 |
To this:
Image | Name1 | Name2 | Name3 | Name4 | Class | Num Detections |
---|---|---|---|---|---|---|
KLF4_Exp1_M012_KO25_MCAO_S1.tif | M012 | KO25 | MCAO | S1 | Left: fiber tracts | 206 |
KLF4_Exp1_M012_KO25_MCAO_S10.tif | M012 | KO25 | MCAO | S10 | Left: fiber tracts | 1632 |
Basically, I need to create 4 columns that record/reproduce the information recorded in the image name.
Any idea of how to perform this in R code? I appreciate your help
Try this
ans <- cbind(df , do.call(rbind , strsplit(df$Image , "[_\\.]"))[,3:6])
colnames(ans) <- c( colnames(ans[1:3]), paste0("Name" , 1:4))
ans <- cbind(ans[c(-2,-3)] , ans[c(2,3)])
Image Name1 Name2 Name3 Name4
1 KLF4_Exp1_M012_KO25_MCAO_S1.tif M012 KO25 MCAO S1
2 KLF4_Exp1_M012_KO25_MCAO_S10.tif M012 KO25 MCAO S10
Class Num.Detections
1 Left:fiber_tracts 206
2 left:fiber_tracts 1632