haotu : an open lab notebook

2018/02/26

replace NA with values from other columns

Filed under: R, R Stats — Tags: , , — S @ 01:27
slf2$Year[is.na(slf2$Year)] <- slf2$Ano[is.na(slf2$Year)]
data <- data.frame('a' = c('A','B','C','D','E'),
 'x' = c(1,2,NA,NA,NA),
 'y' = c(NA,NA,3,NA,NA),
 'z' = c(NA,NA,NA,4,5))

cbind(data[1], mycol = na.omit(unlist(data[-1])))

 

https://stackoverflow.com/questions/14563531/combine-column-to-remove-nas

2018/02/24

set working directory R notebook, markdown

Filed under: R, R Markdown — Tags: , , — S @ 00:08
```{r "setup", include=FALSE}
library(knitr)
opts_knit$set(root.dir = "~/mydir")
```

https://philmikejones.wordpress.com/2015/05/20/set-root-directory-knitr/

2018/01/31

save the first (or any) element of a string split

Filed under: errors in R, Manipulate Data in R, R, R, R Stats — Tags: , — S @ 06:58
dirs<-matrix(ncol = 2,data=unlist(strsplit(out$`Full Name`,split=",")),byrow = TRUE)[,1]

2018/01/03

All Pairwise Combinations of Rownames from a Square Matrix

Filed under: R, R, R spatial, R Stats, Uncategorized — Tags: , , , — S @ 12:37
combn(rownames(my.square.matrix), m=2, FUN=paste, collapse = "-", simplify = T)

2017/07/18

Make ggplot look like base plot in R

Filed under: ggplot, R, R graphics — Tags: , , , , , , — S @ 04:46
myplot + theme_bw() + theme(panel.border = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"))
myplot + theme_bw() + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"))

here

 

 

 

2017/06/22

Automatic MIT License for R Packages

Filed under: errors in R, R, R Stats — Tags: , — S @ 07:01
devtools::use_mit_license()

2016/06/24

save rda data file with compression

Filed under: errors in R, R, R, R Stats — Tags: , , , , — S @ 06:36
save(mydata,file="mydata.rda",compress="xz")

find non-ascii in R

Filed under: errors in R, R, R, R Stats, Uncategorized — Tags: , , , , — S @ 06:34
tools::showNonASCII(readLines("myfiles.R"))

2016/04/18

raster cell area size 1 degree cell size km2

Filed under: R, R spatial, R Stats, Uncategorized — Tags: , — S @ 08:14

For a 1×1 degree cell size raster

r <- raster(ncol=360, nrow=180) #or just the default r<-raster()
area(r)
area(r)$values

 

http://gis.stackexchange.com/questions/177622/r-calculate-raster-cell-size-in-map-units

 

http://gis.stackexchange.com/questions/29734/how-to-calculate-area-of-1-x-1-degree-cells-in-a-raster

2015/02/02

new faster code for PGLMM

Filed under: Uncategorized — Tags: , , — S @ 11:50

https://github.com/willpearse/pez/blob/master/R/pglmm.R

Older Posts »

Blog at WordPress.com.