0

I am a beginner user of R , I have a big CVS file that contain weekly and daily information and I want to read it one time then store the file into small files ( windows ) to deal with them separately.

the original file is a data frame which has Week (integer) and Day (char from Mon to Fri) and other attributes.

as I said I want to store the file into : W1,W2,W3,.....Wn ( the number of weeks depends on the information and I di not now it in advance,but it is between 10-11) moreover I want to store each day information D1,D2,D3,D4,D5

I tried the following code but it did not work as I was expecting.

myclasses = read.csv("C:/myfile.csv") 
i=1
weekdays <- list('Monday','Tuesday','Wednesday','Thursday','Friday')
for (i <= myclasses$Week_number)
{
tmp1 <- paste("W", i, sep = "")
assign(tmp1, myclasses %>% filter(Week_number == i))
j = 'Monday'
for (j in weekdays)
{
tmp2 <- paste("D", j, sep = "")
assign(tmp2, myclasses %>% filter(Week_number == i,Day == j )) 
}
i = (i +1)
}

also I tried for loop , but it led to create a high number of files. just to be clear I want to deal with days windows until create one week window , then the second week's days until create the second week window and so on.

could you help me with that please?

Manal
  • 75
  • 3
  • 12
  • 1
    Just split the data.frame based on week number and save each list element into its own file? Please share a part of your data in an easy-to-paste form. See [this question](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for tips on how to do just that. – Roman Luštrik Sep 26 '17 at 12:02
  • thanks for your reply. This is a part of the head of data Week_number Day hour Hour.Min W_interval interval Sensor_Location 1 1 Monday 7 07:00:00 HS peak S3 2 1 Monday 7 07:00:00 HS peak S1 3 1 Monday 7 07:00:00 HS peak S2 4 1 Monday 7 07:00:00 HS peak S1 – Manal Sep 26 '17 at 12:33
  • what am I trying to do is to split it based on week number then based on day, I want the variables' names to be created automatically – Manal Sep 26 '17 at 12:41
  • I change for (i <= myclasses$Week_number) to while (i<= max(var)) and it works. – Manal Sep 26 '17 at 12:51
  • Please change your original question to include all relevant information. Comment section is not meant for that. – Roman Luštrik Sep 27 '17 at 07:43

1 Answers1

1

I'm taking a stab here based on the input you shown in the comments above. This should allow you to either save the files as individual csv or data.frames.

# this assume the 1st column are row name/id.
dt<-read.table(text="
Week_number Day hour Hour.Min W_interval interval Sensor_Location 
1 1 Monday 7 07:00:00 HS peak S3 
2 1 Monday 7 07:00:00 HS peak S1 
3 1 Monday 7 07:00:00 HS peak S2 
4 1 Monday 7 07:00:00 HS peak S1
5 2 Monday 7 07:00:00 HS peak S1
6 2 Tuesday 7 07:00:00 HS peak S1", header=T)

dt$Day<- as.numeric(dt$Day) #might have to be careful with the order of the dates
#ordered list would be more solid

#splitting based on Week_number and Day column
dt.split1<-split(dt, list(dt$Week_number,dt$Day))

library(stringr) #required for str_sub

#this should save the file as in W"X"D"X".csv in your current directory.
lapply(1:length(dt.split1), function(i) write.csv(dt.split1[[i]], 
                                            file = paste0("W",str_sub(names(dt.split1)[i],1,1),
                                                          "D",str_sub(names(dt.split1)[i],-1), 
                                                          ".csv"),
                                            row.names = FALSE))
#output:W1D1.csv

#alternatively if you want them as data frame.
list2env(dt.split1,envir=.GlobalEnv)
MingH
  • 171
  • 1
  • 2