haotu : an open lab notebook

2018/02/26

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

Advertisements

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

2016/07/14

aggregate by removing NA

Filed under: Manipulate Data in R, R, R Stats, Uncategorized — Tags: — S @ 08:43
na.collapse<-function(x)
{
 x.<-unique(x[!is.na(x)])
   if(length(x.)==0)
 {
   return(NA)
   } else {
     if(length(x.)==1){
     return(x.)
   } else {
     return(paste(x.,collapse="|"))
   }
  }
}
na.collapse(x)

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

add unique id to duplicated grouped rows

Filed under: Manipulate Data in R, R, R, Uncategorized — S @ 12:30
library(plyr)
id(x)
Older Posts »

Blog at WordPress.com.