3.5 Example script


#set working directory
setwd("C:/Users/x/OneDrive/AB202 vår 2019/R")

################ import data and preparations #####
# for data import, make sure that the file path is correct
#import from text file (export as text from excel file first, tabulator deliniated)###
Data_in<-read.table("C:/Users/x/OneDrive/AB202 vår 2019/R/AB202fish_poster.txt", header=TRUE, sep="\t", dec=",")
Data_in<-read.table("C:/Users/x/OneDrive/AB202 vår 2019/R/AB202benthos_poster.txt", header=TRUE, sep="\t", dec=",")
#inspect data, alternatively can also be done via "environment" tab to the left
names(Data_in) #show names of columns
rownames(Data_in) #shows rownames
str(Data_in)#shows vectors for columns and format
#shows data summary with min, max, mean, median
summary(Data_in)

## to be able to work further you might need to change the formats for some columns
## the commands as.numeric, as.dates, as.character and so on is used to set the foramt of your data
# often the date gets importet in the wrong format;
Data_in$Date<-as.Date(Data_in$Date, tz="UTM", format="%d.%m.%Y") #sets date format; you have to define time zone (tz=) and the format (format=)
Data_in2$Date<-as.Date(Data_in2$Date, tz="UTM", format="%d.%m.%Y")
# you mightwant to use julian days or year-days; this can be calculated in R
Data_in$yday<-strptime(Data_in$Date, "%Y-%m-%d")$yday+1 #transform to year-days, which are also might be called julian days
Data_in$yday
Data_in$julian<-julian(Data_in$Date) #transform to julian days couted from 1.1.1960; useful ifyou have a time series with many timepoints over several years
Data_in$julian
#You should also give unique names to each row; 
#for you purposes,a combination of YEAR and STATION would likely work best
forrownames<-as.character(Data_in$Date, tz="UTM", format=" %g") #set date as rowname - you first extract the year and save it in a vector
rownames(Data_in)<-paste(forrownames,Data_in$station,Data_in$Tral_type) #add layer to rownames
rownames(Data_in)
forrownames2<-as.character(Data_in2$Date, tz="UTM", format=" %g") #set date as rowname - you first extract the year and save it in a vector
rownames(Data_in2)<-paste(forrownames2,Data_in2$Station,Data_in2$Method) #add layer to rownames
rownames(Data_in2)

##create several working sheets - this can make the coding later on easier, but is not rrequired; yo ucan subsett later as well
#in case you want to have seperate datasheets for certain trawl type you can subsett your data; you can either use the number or name of the column for subsetting
T_Pelagic<-Data_in[Data_in$Tral_type=="P",] #creating seperate sheet for pelagic trawls; using the column name for subsetting
T_Benthic<-Data_in[Data_in$Tral_type=="B",] #cearting seperate sheet for benthic trawls; using the column name for subsetting 
#or for benthos data:
B_Grab<-Data_in2[1:22,Data_in2$Method=="Grab"] #here i subsett also row 1 to row number 22, since for some reason an extra line was imported which I do not need
B_Trawl<-Data_in2[1:22,Data_in2$Method=="Trawl"] #here i subsett also row 1 to row number 22, since for some reason an extra line was imported which I do not need

##extract variables for metadata (environmental variables) and abundance data
#metadata
B_envir<-Data_in2[c(1:22),c(1:12)] #subsetting the rows (fist vector c()) and columns (vector c() behind ,)by number; this is useful when extracting several columns at once
# taxa-data (response variables)
B_taxa<-Data_in2[c(1:22),c(13:96)] #subsetting the rows (fist vector c()) and columns (vector c() behind ,)by number; this is useful when extracting several columns at once
  Fant du det du lette etter? Did you find this helpful?
[Average: 0]