Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mikelove/r-gotchas
R gotchas
https://github.com/mikelove/r-gotchas
gotchas programming r
Last synced: 9 days ago
JSON representation
R gotchas
- Host: GitHub
- URL: https://github.com/mikelove/r-gotchas
- Owner: mikelove
- Created: 2014-05-28T16:26:13.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2015-07-31T15:44:34.000Z (over 9 years ago)
- Last Synced: 2024-10-13T23:36:27.394Z (23 days ago)
- Topics: gotchas, programming, r
- Language: R
- Homepage:
- Size: 209 KB
- Stars: 46
- Watchers: 5
- Forks: 6
- Open Issues: 0
-
Metadata Files:
- Readme: README.R
Awesome Lists containing this project
README
## ------------------------------------------------------------------------
m <- matrix(1:6, ncol=3)
m
m[1,] # vector
m[,1] # vector
m[1,,drop=FALSE] # matrix## ------------------------------------------------------------------------
df <- data.frame(a=1:3,b=4:6)
df
df[1,] # data.frame
df[,1] # vector
df[,1,drop=FALSE] # data.frame## ------------------------------------------------------------------------
df <- data.frame(a=c("10","11","12"))
as.numeric(df$a) + 1
df <- data.frame(a=c("10","11","12"), stringsAsFactors=FALSE)
as.numeric(df$a) + 1## ------------------------------------------------------------------------
c(TRUE,TRUE,FALSE) & c(FALSE,TRUE,TRUE) # element-wise
c(TRUE,TRUE,FALSE) && c(FALSE,TRUE,TRUE) # just the first
x <- "hi"
is.numeric(x) && x + 1 # evaluates left to right
is.numeric(x) & x + 1 # produces error## ------------------------------------------------------------------------
m <- matrix(1:6, ncol=3)
m^2
apply(m, 2, `^`, 2) # column-wise, ok
apply(m, 1, `^`, 2) # gives back row-wise operation as columns## ------------------------------------------------------------------------
df <- data.frame("test-it-#1"=1:2)
df
make.names("test-it-#1") # this function is used## ------------------------------------------------------------------------
df <- data.frame(a=1:2,b=3:4,c=5:6,d=7:8)
df[,-(2:3)] # numeric index ok
df[,-c("b","c")] # not character index
subset(df, select=-c(b,c)) # by name works here## ------------------------------------------------------------------------
x <- numeric(0)
1:length(x)
seq_len(length(x))
seq_along(x)## ------------------------------------------------------------------------
x <- c(1,2,NA)
which(x == NA)
which(is.na(x))
y <- NULL
y == NULL
is.null(y)## ------------------------------------------------------------------------
m <- matrix(1:4,ncol=2)
write.csv(m,file="matrix.csv")
read.csv("matrix.csv")
read.csv("matrix.csv",row.names=1)## ----,echo=FALSE,results="hide"------------------------------------------
file.remove("matrix.csv")## ------------------------------------------------------------------------
f <- function() {
y <- 1:10
form <- ~ 1
form
}
get("y", environment(f()))