Showing posts with label R. Show all posts
Showing posts with label R. Show all posts

Thursday, April 28, 2016

concatenate two data frame column r

paste(df$c1, df$c2,

Monday, April 25, 2016

Get The Length Of Each Values In A Column

nchar takes a character vector as an argument and returns a vector whose elements contain the sizes of the corresponding elements of x.
nzchar is a fast way to find out if elements of a character vector are non-empty strings.

Why Ifelse Returns Numeric Values?

ock$ref_id2<-ifelse(substr(ock$ref_id,1,4)=="FLT.",substr(ock$ref_id,5,11),ock$ref_id)

because ref_id is factor

Friday, April 22, 2016

How to trim leading and trailing whitespace in R?

Probably the best way is to handle the trailing whitespaces when you read your data file. If you use read.csv or read.table you can set the parameterstrip.white=TRUE.

If you want to clean strings afterwards you could use one of these functions:

# returns string w/o leading whitespace
trim.leading <- function (x)  sub("^\\s+", "", x)

# returns string w/o trailing whitespace
trim.trailing <- function (x) sub("\\s+$", "", x)

# returns string w/o leading or trailing whitespace
trim <- function (x) gsub("^\\s+|\\s+$", "", x)
To use one of these functions on myDummy$country:

 myDummy$country <- trim(myDummy$country)
To 'show' the whitespace you could use:

 paste(myDummy$country)
which will show you the strings surrounded by quotation marks (") making whitespaces easier to spot.


rim {gdata}R Documentation

Remove leading and trailing spaces from character strings

Description

Remove leading and trailing spaces from character strings and other related objects.

Usage

trim(s, recode.factor=TRUE, ...)

bob <- data.frame(lapply(bob, as.character), stringsAsFactors=FALSE)


bob[] <- lapply(bob, as.character)

level sets of factors are different

 a<-ad[trim(ad$airpt_code)!=trim(ad$airpt_metro_code),]
Error in Ops.factor(trim(ad$airpt_code), trim(ad$airpt_metro_code)) :
  level sets of factors are different

Find Columns Which Have A Missing Value

sapply(oa, function(x) sum(is.na(x))

apply(is.na(oa),2,sum)


sapply(oa,function(x) all(is.na(x)))

colnames(oa)[colSums(is.na(oa))>0]


Tuesday, April 19, 2016

Concatenate Two Data Frames

cbind(df1, df2)

cbind() function combines vector, matrix or data frame by columns.


cbind(x1,x2,...)
x1,x2:vector, matrix, data frames


Read in the data from the file:
>x <- read.csv("data1.csv",header=T,sep=",")
>x2 <- read.csv("data2.csv",header=T,sep=",")

>x3 <- cbind(x,x2)
>x3
  Subtype Gender Expression Age     City
1       A      m      -0.54  32 New York
2       A      f      -0.80  21  Houston
3       B      f      -1.03  34  Seattle
4       C      m      -0.41  67  Houston

Monday, April 18, 2016

Aggregate Multiple Columns in R

aggregate(cbind(x1, x2)~year+month, data=df1, sum, na.rm=TRUE)

Monday, April 4, 2016

Lag and Lead Functions in R

lag <- function(x, n = 1L) {
  xlen <- length(x)
  c(rep(NA, n), x[seq_len(xlen - n)])
}

lead <- function(x, n = 1L) {
  xlen <- length(x)
  c( x[-1*seq_len(xlen - n)],rep(NA, n))
}

Sunday, April 3, 2016

Saturday, April 2, 2016

Difference Between R and SAS

You have to be aware that R works very different from the data step in SAS. The lag function in SAS is used in the data step, and is used within the implicit loop structure of that data step. The same goes for the retain function, which simply keeps the value constant when going through the data looping.
R on the other hand works completely vectorized. This means that you have to rethink what you want to do, and adapt accordingly.

Thursday, March 31, 2016

What sapply Stands For?

sapply mean simple lapply. Simplified version of lapply. Both does the same thing except lapply returns a vector while sapply returns a list.

Initialize A Dataframe in R

df <- data.frame(matrix(ncol = 300, nrow = 100))

Wednesday, March 30, 2016

Handling Missing Values in R

is.na(a)


is.na(x1) <- which(x1 == 7)

Recoding Data in R

# recode missing values
A <- c(3, 2, NA, 5, 3, 7, NA, NA, 5, 2, 6)
A[ is.na(A) ] <- 0



#Let’s re-code all values less than 5 to the value 99.
A[ A < 5 ] <- 99
is.na(x1) <- which(x1 == 7)

Tuesday, March 29, 2016

Random Forests In A Nutshell

Random forests improve predictive accuracy by generating a large number of bootstrapped trees (based on random samples of variables), classifying a case using each tree in this new "forest", and deciding a final predicted outcome by combining the results across all of the trees (an average in regression, a majority vote in classification). Breiman and Cutler's random forest approach is implimented via therandomForest package.

Here is an example.
# Random Forest prediction of Kyphosis data
library(randomForest)
fit <- randomForest(Kyphosis ~ Age + Number + Start,   data=kyphosis)
print(fit) # view results 
importance(fit) # importance of each predictor

Regression Model Strategy

Diagnostics
Transformation
Variable Selection
Diagnostics

Plotting A Mathematical Expression


plotcurve <-
  function(equation = "y = sqrt(1/(1+x^2))", ...){
    leftright <- strsplit(equation, split = "=")[[1]]
    left <- leftright[1] # The part to the left of the "="
    right <- leftright[2] # The part to the right of the "="
    expr <- parse(text=right)
    xname <- all.vars(expr)
    if(length(xname) > 1)stop(paste("There are multiple variables, i.e.",paste(xname,
                                                                               collapse=" & "),
                                    "on the right of the equation"))
    if(length(list(...))==0)assign(xname, 1:10)
    else {
      nam <- names(list(...))
      if(nam!=xname)stop("Clash of variable names")
      assign("x", list(...)[[1]])
      assign(xname, x)
    }
    y <- eval(expr)
    yexpr <- parse(text=left)[[1]]
    xexpr <- parse(text=xname)[[1]]
    plot(x, y, ylab = yexpr, xlab = xexpr, type="n")
    lines(spline(x,y))
    mainexpr <- parse(text=paste(left, "==", right))
    title(main = mainexpr)
  }

plotcurve()
plotcurve("ang=asin(sqrt(p))", p=(1:49)/50)

Searching R Functions For A Specified Token

grep<-function(str){
  tempobj<-ls(envir=sys.frame(-1))
  objstring<-character(0)
  for(i in tempobj) {
    myfunc<-get(i)
    if(is.function(myfunc))
      if(length(grep(str,deparse(myfunc))))
        objstring<-c(objstring,i)
  }
  return(objstring)
}

mygrep("for")

Saturday, March 26, 2016

Applying The Same Operation On Multiple Data Frames In R

Solution #1:

a<-c(1,2,3,4)
b<-c(5,6,7,8)
pos1<-data.frame(cbind(a,b))
pos2<-pos1+1
pos3<-pos1+2

var1<-paste0('pos',1:3)
exps<-paste0(var1,'$x<-',var1,'$a+10')

for(exp in exps){
  eval(parse(text=exp))
}

Solution #2: Use lapply

x<-list(pos1,pos2,pos3)

lapply(x, function(x) x$x<-x$a+10)

lapply(x,function(x)cor(x[,2],x[,3]))


http://stackoverflow.com/questions/16115745/applying-a-set-of-operations-across-several-data-frames-in-r

http://stackoverflow.com/questions/19249303/applying-lapply-on-multiple-data-frames-in-a-list-r

https://www.datacamp.com/community/tutorials/r-tutorial-apply-family