library(readr) library(numbers) PAXMIN_GH_weekday_count_state <- read_csv("Z:/My Documents/PolyU OneDrive/NHANES 2011-2014/PAXMIN_GH_weekday_count_state.csv") PAXHD_GH <- read_csv("Z:/My Documents/PolyU OneDrive/NHANES 2011-2014/PAXHD_GH.csv") ### create an array to store accelerometer data accelerometer.array <- array(0,dim=c(nrow(PAXHD_GH),8640,4)) ### select accelerometer data according to SEQN accelerometer.array[1,1,1] <- as.integer(PAXMIN_GH_weekday_count_state[1,2]) accelerometer.array[1,1,2] <- as.numeric(PAXMIN_GH_weekday_count_state[1,3]) accelerometer.array[1,1,3] <- as.integer(PAXMIN_GH_weekday_count_state[1,4]) temp.n.counter <- 1 temp.counter <- 1 store.seqn <- rep(0,nrow(PAXHD_GH)) store.seqn[1] <- as.integer(PAXMIN_GH_weekday_count_state[1,1]) for (i in 2:nrow(PAXMIN_GH_weekday_count_state)){ if (as.integer(PAXMIN_GH_weekday_count_state[i,1]) == as.integer(PAXMIN_GH_weekday_count_state[i-1,1])){ accelerometer.array[temp.n.counter,temp.counter,1] <- as.integer(PAXMIN_GH_weekday_count_state[i,2]) accelerometer.array[temp.n.counter,temp.counter,2] <- as.numeric(PAXMIN_GH_weekday_count_state[i,3]) accelerometer.array[temp.n.counter,temp.counter,3] <- as.integer(PAXMIN_GH_weekday_count_state[i,4]) temp.counter <- temp.counter+1 } else { temp.counter <- 1 temp.n.counter<-temp.n.counter+1 print(temp.n.counter) store.seqn[temp.n.counter] <- as.integer(PAXMIN_GH_weekday_count_state[i,1]) accelerometer.array[temp.n.counter,1,1] <- as.integer(PAXMIN_GH_weekday_count_state[i,2]) accelerometer.array[temp.n.counter,1,2] <- as.numeric(PAXMIN_GH_weekday_count_state[i,3]) accelerometer.array[temp.n.counter,1,3] <- as.integer(PAXMIN_GH_weekday_count_state[i,4]) } } ### save individual accelerometer file write.csv(store.seqn, file = "Z:/My Documents/PolyU OneDrive/NHANES 2011-2014/seqn.csv", row.names=FALSE) for (i in 1:nrow(PAXHD_GH)){ print(i) write.csv(accelerometer.array[i,,], file = paste("Z:/My Documents/PolyU OneDrive/NHANES 2011-2014/accel_indiv/",paste(store.seqn[i],"csv",sep="."),sep=""), row.names=FALSE) } ### read individual csv file store.seqn.read <- read.csv("Z:/My Documents/PolyU OneDrive/NHANES 2011-2014/seqn.csv", header=TRUE) files <- list.files(path = "Z:/My Documents/PolyU OneDrive/NHANES 2011-2014/accel_indiv/", pattern = "*.csv", full.names = T) accel.read <- list() for (i in 1:length(files)){ print(i) accel.read[[i]] <- read.csv(files[i]) } ### remove the 0s in individual csv file, and save individual csv file without 0s for (i in 1:length(files)){ print(i) write.csv(subset(accel.read[[i]],V1!=0), file = paste("Z:/My Documents/PolyU OneDrive/NHANES 2011-2014/accel_indiv/",paste(store.seqn.read[i,1],"csv",sep="."),sep=""), row.names=FALSE) } ### process summary sleep data for individuals ### create dataset to store it sleep.summary <- array(0,dim=c(length(files),101)) sleep.summary[,1] <- store.seqn.read[1:length(files),1] for (i in 1:length(files)){ print(i) # if first 15 minutes are sleep then the subject is first sleeping (status=1), first awake (status=0, default) otherwise if (accel.read[[i]][1,3]==2 & accel.read[[i]][2,3]==2 & accel.read[[i]][3,3]==2 & accel.read[[i]][4,3]==2 & accel.read[[i]][5,3]==2 & accel.read[[i]][6,3]==2 & accel.read[[i]][7,3]==2 & accel.read[[i]][8,3]==2 & accel.read[[i]][9,3]==2 & accel.read[[i]][10,3]==2 & accel.read[[i]][11,3]==2 & accel.read[[i]][12,3]==2 & accel.read[[i]][13,3]==2 & accel.read[[i]][14,3]==2 & accel.read[[i]][15,3]==2) { accel.read[[i]][1,4] <- 1 accel.read[[i]][2,4] <- 1 accel.read[[i]][3,4] <- 1 accel.read[[i]][4,4] <- 1 accel.read[[i]][5,4] <- 1 accel.read[[i]][6,4] <- 1 accel.read[[i]][7,4] <- 1 accel.read[[i]][8,4] <- 1 accel.read[[i]][9,4] <- 1 accel.read[[i]][10,4] <- 1 accel.read[[i]][11,4] <- 1 accel.read[[i]][12,4] <- 1 accel.read[[i]][13,4] <- 1 accel.read[[i]][14,4] <- 1 accel.read[[i]][15,4] <- 1 } for (j in 17:nrow(accel.read[[i]])){ if (accel.read[[i]][j-15,3]==1 & # change status from awake to sleep for 15 consecutive minutes of awake accel.read[[i]][j-14,3]==1 & accel.read[[i]][j-13,3]==1 & accel.read[[i]][j-12,3]==1 & accel.read[[i]][j-11,3]==1 & accel.read[[i]][j-10,3]==1 & accel.read[[i]][j-9,3]==1 & accel.read[[i]][j-8,3]==1 & accel.read[[i]][j-7,3]==1 & accel.read[[i]][j-6,3]==1 & accel.read[[i]][j-5,3]==1 & accel.read[[i]][j-4,3]==1 & accel.read[[i]][j-3,3]==1 & accel.read[[i]][j-2,3]==1 & accel.read[[i]][j-1,3]==1 & mod(accel.read[[i]][j-1,4],2)==0) { accel.read[[i]][(j-15):nrow(accel.read[[i]]),4] <- rep((accel.read[[i]][j-1,4]+1),(nrow(accel.read[[i]])-j+16)) } if (accel.read[[i]][j-15,3]==1 & # change status from sleep to awake for 15 consecutive minutes of sleep accel.read[[i]][j-14,3]==2 & accel.read[[i]][j-13,3]==2 & accel.read[[i]][j-12,3]==2 & accel.read[[i]][j-11,3]==2 & accel.read[[i]][j-10,3]==2 & accel.read[[i]][j-9,3]==2 & accel.read[[i]][j-8,3]==2 & accel.read[[i]][j-7,3]==2 & accel.read[[i]][j-6,3]==2 & accel.read[[i]][j-5,3]==2 & accel.read[[i]][j-4,3]==2 & accel.read[[i]][j-3,3]==2 & accel.read[[i]][j-2,3]==2 & accel.read[[i]][j-1,3]==2 & mod(accel.read[[i]][j-1,4],2)==1) { accel.read[[i]][(j-15):nrow(accel.read[[i]]),4] <- rep((accel.read[[i]][j-1,4]+1),(nrow(accel.read[[i]])-j+16)) } } # store sleep summary for (j in 1:100){ sleep.summary[i,(j+1)] <- sum(accel.read[[i]][,4]==j) } } ### save file with sleep status for (i in 1:length(files)){ print(i) write.csv(subset(accel.read[[i]],V1!=0), file = paste("Z:/My Documents/PolyU OneDrive/NHANES 2011-2014/accel_indiv/",paste(store.seqn.read[i,1],"csv",sep="."),sep=""), row.names=FALSE) } ### save sleep summary write.csv(sleep.summary, file = "Z:/My Documents/PolyU OneDrive/NHANES 2011-2014/sleep.summary.csv", row.names=FALSE)