Last active
October 14, 2016 08:36
-
-
Save tomatoiscoding/061f810c06ec99002459438abab340cd to your computer and use it in GitHub Desktop.
some tricks for regular expression in R
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # convert charactor vector to a matrix | |
| a <- rep(NA, 2) # generate a vector | |
| a[1] <- '1, 2, 3, 4' | |
| a[2] <- '5, 6, 7, 8' | |
| a <- gsub('\\s', '', a) | |
| b <- matrix(NA, length(a), 4) | |
| for(i in 1:length(a)) { | |
| b[i, ] <- as.numeric(as.matrix(strsplit(a[i], ',')[[1]])) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # read character with scan() | |
| scan(file = 'path/to/file', what = "character") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| text <- c("1", "2", "3") | |
| paste(text, collapse = '') # "123" | |
| text <- as.numeric(as.matrix(text)) # [1] 1 2 3 | |
| t <- c("1 2 3") | |
| gsub("\\s", "", t) # "123" | |
| gsub("[[:space:]]", "", t) # "123" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| con <- file('path/file.txt','r') | |
| test <- readLines(con)[[1]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment