Advertisement
Guest User

Untitled

a guest
Jun 15th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. #setting package and directory
  2. library(plyr)
  3. library(stringr)
  4. setwd("/Users/macbook/Desktop/Project_Folder/File_folder1")
  5.  
  6. #Creating a list of all the filenames:
  7. filenames <- list.files(path = "/Users/macbook/Desktop/Project_Folder/File_folder1")
  8.  
  9. #creating a function to read csv and in the same time adding an additional column with the name of the file
  10. read_csv_filename <- function(filename)
  11. {
  12. ret <- read.csv(filename, header=TRUE, sep=",")
  13. ret$Source <- filename #EDIT
  14. ret
  15. }
  16.  
  17. #importing
  18. import <- ldply(filenames, read_csv_filename)
  19.  
  20. #making a copy of import
  21. data<-import
  22.  
  23. #modifying the file name so it removes ".csv" and change the header
  24. data$Source<-str_sub(data$Source, end=-5)
  25. data[1,3]<-"date_expnb_pulsenb"
  26. t<-substr(data[1,3],1,3)
  27. head(data, n=10)
  28.  
  29. #create a column with the experiment number, extracted from the file name
  30. data$expnb<-substr(data$Source, 10, 13)
  31. data$expnb<-as.numeric(data$expnb)
  32. head(data, n=10)
  33. tail(data, n=10)
  34.  
  35. > head(data, n=10)
  36. Time Channel.A Source pulsenb expnb
  37. 1 (us) (A) expnb_pulsenb NA NA
  38. 2 -20.00200030 -0.29219970 20190409-0001_002 2 1
  39. 3 -20.00100030 -0.29219970 20190409-0001_002 2 1
  40.  
  41. > tail(data, n=10)
  42. Time Channel.A Source pulsenb expnb
  43. 20800511 179.99199405 -0.81815930 20190409-0001_105 105 1
  44. 20800512 179.99299405 -0.81815930 20190409-0001_105 105 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement