haotu : an open lab notebook

2018/02/26

r remove all objects except

Filed under: R — Tags: — S @ 02:08
rm(list=setdiff(ls(), "x"))

https://stackoverflow.com/questions/6190051/how-can-i-remove-all-objects-but-one-from-the-workspace-in-r

Advertisements

extract numeric number from character string

Filed under: Manipulate Data in R, R — Tags: — S @ 01:29
library(stringr)
str_extract(data, "[[:digit:]]+")

 

https://stackoverflow.com/questions/15451251/extract-numeric-part-of-strings-of-mixed-numbers-and-characters-in-r

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/02/01

R Markdown Resources

Filed under: R, R Markdown, R Stats — Tags: , , , , — S @ 09:10

cheatsheets here https://www.rstudio.com/resources/cheatsheets/

online tutorial

http://rmarkdown.rstudio.com/lesson-1.html

chapter in r for data science

http://r4ds.had.co.nz/r-markdown.html

 

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/29

Replace . in string in R

Filed under: errors in R, Manipulate Data in R, R, R, R Stats — Tags: — S @ 07:54
gsub(x = ff,pattern = "[.]",replacement="")

2018/01/25

dplyr column names with spaces, how to index

Filed under: Manipulate Data in R, R, R, R Stats — Tags: , , — S @ 09:12

backtick it!

`my name`

https://stackoverflow.com/questions/22842232/dplyr-select-column-names-containing-white-space

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/12/11

read file from parent folder of current working directory

Filed under: Manipulate Data in R, R, R, R Stats, Uncategorized — Tags: , — S @ 08:10

If  your working directory is C:Parent/Child

Then to read a file from Parent use ..

file="../MY_FILE.RData"

https://stackoverflow.com/questions/24319168/how-to-move-backward-parent-folder

Older Posts »

Create a free website or blog at WordPress.com.