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

Thursday, 17 December 2015

Appending Items to List in R

A very common programming problem is that a function must return a list of objects (itmes like trees...).
In R, we can do something like the following:

func_????? <- function ( ... )
{
    ret_TreeList = list()

    for(i in 1: ?????)
    {
        if(...)
        {
             ret_TreeList[[i]] = something
        }
        else(...)
        {
             ret_TreeList[[i]] = something else
        } 
        
    }

    return(ret_TreeList)
}

Then we cna use it like this:

>TreeList = func_?????(...)
>TreeList[3]

Hope it helps ;-}

Wednesday, 16 December 2015

Install R packages Removed from CRAN (from the source)

I need to use some really useful R packages but unfortunately they are removed from CRAN which means I cannot just install.packages(...) from the cloud and I must install it from the source.

There are two ways of doing this:

(1)
library(devtools)
install_url('http://cran.r-project.org/src/contrib/Archive/dynamo/dynamo_0.1.3.tar.gz')
install_url('http://cran.r-project.org/src/contrib/Archive/gafit/gafit_0.4.tar.gz')

(2) Which I prefer!!
Download the package source first and then:
install.packages('/path/dynamo_0.1.3.tar.gz', type = 'source')

ref:
http://stackoverflow.com/questions/18655976/how-to-install-the-package-that-has-been-removed-from-the-cran-repository-in-r-e

Sunday, 13 December 2015

Where is to find the bodyfat dataset?

When trying some old (even not so old) examples using R package mboost, the dataset "bodyfat" cannot be loaded.

> data("bodyfat", package = "mboost")
Warning message:

In data("bodyfat", package = "mboost") : data set ‘bodyfat’ not found

Many famous examples are still like this, like
Yanchang Zhao's book: “R and Data Mining: Examples and Case Studies”

==============================================================

The data is provided in package TH.data, but no longer in package mboost.

To use the data, run code below.

data("bodyfat", package="TH.data")

Saturday, 14 November 2015

Find the k Smallest Elements in a List using R

This program is definitely not the most efficient way to find the k smallest elements in a List. But I simply need this now. Improvements might be done later and you are mostly welcome to share any ideas of improvements.

l = c(7,3,6,1,0,2,10,9)

KSmallestElem <- function(DataList, k)
{
  n = length(DataList)
  kmin = {}
  
  initMinIndex = which.min(DataList)
  initMinVal = min(DataList)
  kmin = rbind(kmin,c(initMinIndex,initMinVal))
  
  # 1st level loop:
  #     k times to find the smallest k elemenst
  for(i in 1:k)
  {
    minIndex = 1
    minValue = DataList[1]
    # 2nd level loop:
    #     over the original DataList
    for(j in 1:n)
    {
      if(DataList[j] > kmin[i,2] &&
         DataList[j] < minValue)
      {
        minIndex = j
        minValue = DataList[j]
      }
      
    }
    
    kmin = rbind(kmin,c(minIndex,minValue))
  }
  
  return(kmin)
}


KSmallestElem(l,3)

#################################
Results:
> KSmallestElem(l,3)
     [,1] [,2]
[1,]    5    0
[2,]    4    1
[3,]    6    2
[4,]    2    3









Saturday, 7 November 2015

Reverse scaling data/ Unscaling data in R

In machine learning, data should usually be scaled before feeding to the training model since variables might be in different kinds of ranges. If not scaled, it shall make the prediction far from accuracy when calculating the distances among  data.

However, we still need a prediction result in the original range, which is the reverse of scaling.

In r, we could do this:

attributes(d$s.x)

d$s.x * attr(d$s.x, 'scaled:scale') + attr(d$s.x, 'scaled:center')
for example:
> x <- 1:10
> s.x <- scale(x)
> s.x
            [,1]
 [1,] -1.4863011
 [2,] -1.1560120
 [3,] -0.8257228
 [4,] -0.4954337
 [5,] -0.1651446
 [6,]  0.1651446
 [7,]  0.4954337
 [8,]  0.8257228
 [9,]  1.1560120
[10,]  1.4863011
attr(,"scaled:center")
[1] 5.5
attr(,"scaled:scale")
[1] 3.02765
> s.x * attr(s.x, 'scaled:scale') + attr(s.x, 'scaled:center')
      [,1]
 [1,]    1
 [2,]    2
 [3,]    3
 [4,]    4
 [5,]    5
 [6,]    6
 [7,]    7
 [8,]    8
 [9,]    9
[10,]   10
attr(,"scaled:center")
[1] 5.5
attr(,"scaled:scale")
[1] 3.02765



ref:
http://stackoverflow.com/questions/10287545/backtransform-scale-for-plotting

If it helps, you could also try the R library:
http://www.inside-r.org/packages/cran/DMwR/docs/unscale



Thursday, 29 October 2015

Error in readRDS(file) : error reading from the connection

I met this error when I was installing the package "stringr". It is one of the dependencies if you want to install "ggplot2" package. I have this problem probably because my computer was interrupted when I was installing "ggplot2" package. But that's just a guess.

Right after the error, it prompts : "TRY removing /usr/local/lib/R/site-library/00LOCK-stringi/". So I just sudo went to that directory and rm -rf 00LOCK-stringi.

I solved the problem this way. But there are a popular ref online:
        http://stackoverflow.com/questions/6473831/readrdsfile-in-r

Try it if my solution doesn't work for you,

Wednesday, 28 October 2015

Deal with Missing Data NA in R

Firstly, Yes, there is no " " on NA.

I wrote something like
                   if(mat[i,j] == "NA"){...}

Please don't laugh at me R users...

I got my answer from the following post:
 http://stackoverflow.com/questions/27018471/r-missing-value-where-true-false-needed

comments = c("no","yes",NA)
for (l in 1:length(comments)) {
    if (!is.na(comments[l])) print(comments[l])
}
[1] "no"
[1] "yes"
So the function is.na is the way to do the judgement. And yes use NA directly, no need to quote.

Sunday, 25 October 2015

R problem of installing packages: package ‘xxxx’ is not available (for R version 3.2.2) and unable to access index for repository xxxx

I met this problem after re-installed the Ubuntu. I've got to set up the whole programming environment again...  :-{{

--- Please select a CRAN mirror for use in this session ---
Warning: unable to access index for repository http://cran.utstat.utoronto.ca/src/contrib
Warning message:
package ‘xxxx’ is not available (for R version 3.2.2) 

Checked something online. The following two helped...

http://stackoverflow.com/questions/25721884/how-should-i-deal-with-package-xxx-is-not-available-for-r-version-x-y-z-wa
http://stackoverflow.com/questions/25599943/unable-to-install-packages-in-latest-version-of-rstudio-and-r-version-3-1-1

Basically, just change to another mirror...I changed to one in US, didn't work either... Then Canada(BC), WORKed!!!!

Wired...but you can try the same cuz there might be many problems to the unavailability of a university server, which is usually the case for R mirrors...

Happy coding!

Friday, 16 October 2015

It's because your R is too old

I just want to install ggplot2 for R. But it says I don't have reshape2. Then I tried to install reshape2 but it says
" plyr package for R not available for R version 3.0.2"
I thought this is because the R version is too new. Somebody said and I tried download the plyr package and install. FINALLY I GET:
"ERROR: this R is version 3.0.2, package 'plyr' requires R >= 3.1.0"
So, My R version is too old!!!!

I searched a lot. Only the following worked.
       http://askubuntu.com/questions/218708/installing-latest-version-of-r-base
       http://stackoverflow.com/questions/16093331/how-to-install-r-version-3
=======================================================================
This is an old post and I wrote it when it was impossible to install R with simple (it was installing old 2.15 version):
sudo apt-get install r-base-core
Right now you do not need to do all written below and can simply use the abovementioned command. At the time of updating it will give you 3.1 version.
Here is previous post
Uninstall old R
sudo apt-get remove r-base-core
Open sources.list
sudo nano /etc/apt/sources.list    
and add deb to it
deb http://cran.rstudio.com/bin/linux/ubuntu precise/
precise is your ubuntu name (may be different)
"precise" is 12.04. My ubuntu is 14.04. It should be "trusty"
Add key to sign CRAN packages
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E084DAB9
Add specific PPA to the system
sudo add-apt-repository ppa:marutter/rdev
sudo apt-get update
sudo apt-get upgrade
installing
sudo apt-get install r-base
From R to check version
version
should give you
R version 3.2.2 (2015-08-14)
Hope people will find this helpful