Skip to content

Commit

Permalink
from wide to long
Browse files Browse the repository at this point in the history
applicable to any dataset
  • Loading branch information
aakbarie committed May 16, 2015
1 parent 7c3bc11 commit 1fdb262
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions from wide to long in R for NZ.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
library(tidyr)

meta_data = read.csv("C:\\Users\\Akbar Akbari Esfahan\\Google Drive\\Mikes\\Code-for-NZ\\layer_depth.csv",header=T)
setwd("C:\\Users\\Akbar Akbari Esfahan\\Google Drive\\Mikes\\Code-for-NZ")
files <- list.files(pattern=".csv")
files

create_f = function(file)
{
data = as.data.frame(lapply(file, read.csv,header=T))
dim(data)
temp1 = data[,1:24]
data_long <- gather(temp1, depth, resistivity, res1:res21)
data_long$depth = as.character(data_long$depth)

for(i in 1:length(meta_data[,1]))
{
data_long$Layer[data_long$depth == meta_data[i,3]] = meta_data[i,1]
data_long$Thick[data_long$depth == meta_data[i,3]] = meta_data[i,2]
data_long$depth[i]=data_long$DEM[i] - data_long$Thick[i]
}

write.csv(data_long,paste(file,"_short.csv"),row.names=F)
#For all res values
data_long <- gather(data, depth, resistivity, res1:res24)
data_long$depth = as.character(data_long$depth)

for(i in 1:length(meta_data[,1]))
{
data_long$Layer[data_long$depth == meta_data[i,3]] = meta_data[i,1]
data_long$Thick[data_long$depth == meta_data[i,3]] = meta_data[i,2]
data_long$depth[i]=data_long$DEM[i] - data_long$Thick[i]
}


write.csv(data_long,paste(file,"_long.csv"),row.names=F)

}

create_f("Morrill_block_test_75%(2)_voxler.csv")
create_f("Morrill_block_test_75%(1)_voxler.csv")
create_f("Morrill_block_test_known_75%(2)_voxler.csv")
create_f("Morrill_block_test_known_75%(1)_voxler.csv")

0 comments on commit 1fdb262

Please sign in to comment.