Skip to content

Instantly share code, notes, and snippets.

Created July 18, 2013 09:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/bfa8b229622555ba8a77 to your computer and use it in GitHub Desktop.
Save anonymous/bfa8b229622555ba8a77 to your computer and use it in GitHub Desktop.
#Testing Dataset
DataSet_df <- structure(list(V1 = c("Date", "20120101", "20120101", "20120101",
"20120101", "20120102", "20120102", "20120102", "20120102", "20120103",
"20120103", "20120103", "20120103", "20120101", "20120101", "20120101",
"20120101", "20120102", "20120102", "20120102", "20120102", "20120103",
"20120103", "20120103", "20120103"), V2 = c("Country", "US",
"US", "US", "US", "US", "US", "US", "US", "US", "US", "US", "US",
"KR", "KR", "KR", "KR", "KR", "KR", "KR", "KR", "KR", "KR", "KR",
"KR"), V3 = c("ID", "1", "2", "3", "4", "1", "2", "3", "4", "1",
"2", "3", "4", "1", "2", "3", "4", "1", "2", "3", "4", "1", "2",
"3", "4"), V4 = c("Fruit", "Apple", "Orange", "Banana", "Melon",
"Apple", "Apple", "Banana", "Melon", "Apple", "Apple", "Banana",
"Apple", "Orange", "Orange", "Orange", "Orange", "Apple", "Apple",
"Banana", "Melon", "Apple", "Apple", "Banana", "Apple"), V5 = c("Favorite",
"Book", "Knife", "Watch", "Phone", "Phone", "Knife", "Watch",
"Phone", "Watch", "Knife", "Watch", "Phone", "Book", "Knife",
"Phone", "Phone", "Phone", "Knife", "Watch", "Phone", "Watch",
"Knife", "Watch", "Phone"), V6 = c("Money", "100", "150", "80",
"90", "120", "140", "130", "100", "70", "150", "200", "110",
"200", "30", "190", "60", "120", "140", "130", "100", "70", "150",
"200", "110")), .Names = c("V1", "V2", "V3", "V4", "V5", "V6"
), class = "data.frame", row.names = c(NA, -25L))
DateList <- unique(DataSet_df[2:nrow(DataSet_df),1])
CtryList <- unique(DataSet_df[2:nrow(DataSet_df),2])
FieldList <- DataSet_df[1,4:6]
output_all <- data.frame(matrix(nrow=0,ncol=7))
colnames(output_all) <- c("Date","Country","Field","ID 1","ID 2","ID 3","ID 4")
for ( i in 1:length(DateList) ) {
TargetDate <- DateList[i]
for ( j in 1:length(CtryList) ) {
TargetCtry <- CtryList[j]
TargetDataSet <- DataSet_df[DataSet_df[,1] == TargetDate & DataSet_df[,2] == TargetCtry,]
TargetFieldData <- t(TargetDataSet[,4:6])
DateCol <- rep(TargetDate,nrow(TargetFieldData))
CtryCol <- rep(TargetCtry,nrow(TargetFieldData))
FieldCol <- t(FieldList)
output_indv <- cbind(DateCol,CtryCol,FieldCol,TargetFieldData)
colnames(output_indv) <- c("Date","Country","Field","ID 1","ID 2","ID 3","ID 4")
output_all <- rbind(output_all,output_indv)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment