-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpecies_Counts_to_Binary_11.5.2020.R
33 lines (28 loc) · 1.95 KB
/
Species_Counts_to_Binary_11.5.2020.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
## This function will turn your site x species matrix into a presence/absence table!
counts_to_binary <- function(dataFrame){
new_m <- matrix(nrow=dim(dataFrame)[1],ncol = dim(dataFrame)[2]) # create new matrix w/ same rows and cols as input dataframe
for( currentRow in 1:nrow(dataFrame)){ # for every row
for( currentCol in 1:ncol(dataFrame)){ # for every column
if ( is.na(dataFrame[currentRow, currentCol]) & is.numeric(dataFrame[currentRow, currentCol])){ # if both row and col (specifies each cell) are NA, change val to 0
new_m[currentRow, currentCol] = 0
} else if( is.numeric(dataFrame[currentRow, currentCol]) & dataFrame[currentRow, currentCol] > 0){ # if both row and col (specifies each cell) are > 0, change val to 1
new_m[currentRow, currentCol] = 1
} else if ( is.numeric(dataFrame[currentRow, currentCol]) & dataFrame[currentRow, currentCol] == 0){ # if both row and col (specifies each cell) == 0 , change val to 0
new_m[currentRow, currentCol] = 0
} else if ( is.character(dataFrame[currentRow, currentCol])){ # if both row and col (specifies each cell) == 0 , change val to 0
new_m[currentRow, currentCol] = dataFrame[currentRow, currentCol]
}
}
}
new_df <- as.data.frame(new_m) #turns matrix into dataframe
names(new_df) <- names(dataFrame) #names rows & cols of new dataframe to be same as row names and col names from input dataframe
rownames(new_df) <- rownames(dataFrame)
new_df2=new_df[,order(ncol(new_df):1)]
new_df2=new_df2[rownames(dataFrame),colnames(dataFrame)]
return(new_df2) # ensures only output is the new dataframe
}
## Function usage looks like this
# new_df2 <-counts_to_binary(dataFrame)
## Can confirm if new dataframe rows and columns match input dataframe with identical()
## Try: identical (colnames(name of new_df2),colnames(name of dataFrame))
### new_df2 -- whatever you call your new dataframe; dataFrame -- your input dataframe for the function