Categories
status

Retrieving rda into a different variable name in…

Retrieving .rda into a different variable name in R

1. Using environment

x <- 1
save(x, file = 'saved_x.rda')
Data.Env <- new.env()
load(file = 'saved_x.rda')
y <- Data.Env$x

2. Using saveRDS, readRDS for a single object

x <- 1
saveRDS(x, file = 'saved_x.rds')
y <- readRDS('saved_x.rds')
Categories
status

Convert a list of data frame into a…

Convert a list of data frame into a data frame
ldply in plyr package did awesome job.

I have a list with different length of data. I’d like to convert the list to a data frame with the name of the each list showing as a column. Then I can do grouping the data.
Here is an example

> test.table
$a
1 2 3 
1 1 1 
$b
1 2 3 4 
2 2 1 1 
$c
1 
1 
$d
1 3 4 
1 1 1 
$e
7 
1 
> test.table.df <- ldply(test.table, data.frame)
> test.table.df
   .id Var1 Freq
1    a    1    1
2    a    2    1
3    a    3    1
4    b    1    2
5    b    2    2
6    b    3    1
7    b    4    1
8    c    1    1
9    d    1    1
10   d    3    1
11   d    4    1
12   e    7    1

Then I can do summary or grouping the data

> tapply(test.table.df$Freq, test.table.df$Var1, sum)
1 2 3 4 7 
5 3 3 2 1 

or

> ddply(test.table.df, 'Var1', function(x) sum(x$Freq))
  Var1 V1
1    1  5
2    2  3
3    3  3
4    4  2
5    7  1

Final touch. As the grouping variable, Var1 in this example is factor. So if you want to use the level names or labels instead of levels, then you need to convert the type.

> as.numeric(levels(test.count.df$Var1))[test.count.df$Var1]
[1] 1 2 3 4 7
> test.count.df$dist <- with(test.count.df, as.numeric(levels(Var1))[Var1])                                                                                   
> test.count.df
  Var1 V1 dist
1    1  5    1
2    2  3    2
3    3  3    3
4    4  2    4
5    7  1    7

http://stackoverflow.com/questions/2851327/r-converting-a-list-of-data-frames-into-one-data-frame
http://tolstoy.newcastle.edu.au/R/help/03a/6325.html

Categories
status

Do NOT use 1 length x in for…

Do NOT use 1:length(x) in for statement in R.
Because when x is empty, the loop will run over 1:0, which is twice.

> y <- as.numeric()
> length(y)
[1] 0
> for (i in 1:length(y)) { print("y")}
[1] "y"
[1] "y"
> for (i in 1:seq(along=y)) { print("y")}
Error in 1:seq(along = y) : argument of length 0
Categories
status

with within and transform in R I found…

with, within, and transform in R.
I found that they are useful to deal with data set.
I don’t quite understand the explanation in the help page and I can’t not guarantee the correctness of my explanation so I provide some examples to show their behavior.
Fist of all, they need data frame or list but not matrix. These are three examples to add a column which is the sum of the first two columns. See the differences among the functions.

  • with: returns one column
  • within: returns the whole data
  • transform: returns the whole data but the function argument is slightly different.

with

testwith <- data.frame(x1 = 1:10, x2 = 11:20)
> testwith$y <- with(testwith, {x1 + x2})
> testwith
   x1 x2  y
1   1 11 12
2   2 12 14
3   3 13 16
4   4 14 18
5   5 15 20
6   6 16 22
7   7 17 24
8   8 18 26
9   9 19 28
10 10 20 30

within

> testwith <- data.frame(x1 = 1:10, x2 = 11:20)
> testwith <- within(testwith, {y <- x1 + x2})
> testwith
   x1 x2  y
1   1 11 12
2   2 12 14
3   3 13 16
4   4 14 18
5   5 15 20
6   6 16 22
7   7 17 24
8   8 18 26
9   9 19 28
10 10 20 30

transform

> testwith <- data.frame(x1 = 1:10, x2 = 11:20)
> testwith <- transform(testwith, y = x1 + x2)
> testwith
   x1 x2  y
1   1 11 12
2   2 12 14
3   3 13 16
4   4 14 18
5   5 15 20
6   6 16 22
7   7 17 24
8   8 18 26
9   9 19 28
10 10 20 30

Some more examples at
http://stackoverflow.com/questions/1310247/in-r-do-you-use-attach-or-call-variables-by-name-or-slicing

Categories
status

Reverse search in RStudio Type part of a…

Reverse search in RStudio
Type part of a command and Ctrl-Up arrow will show the previous commands history.

Categories
status

Use t using apply to apply a non…

Use t() using apply() to apply a non-aggregate function on a matrix row wise because column wise is the default order of matrix in R.

Example.
I’d like to reverse the order of elements in each row.

> test.m <- matrix(1:20, nrow = 4)
> test.m
[,1] [,2] [,3] [,4] [,5]
[1,]    1    5    9   13   17
[2,]    2    6   10   14   18
[3,]    3    7   11   15   19
[4,]    4    8   12   16   20

Apply will put the results in column wise manner.

> apply(test.m, 1, rev)
[,1] [,2] [,3] [,4]
[1,]   17   18   19   20
[2,]   13   14   15   16
[3,]    9   10   11   12
[4,]    5    6    7    8
[5,]    1    2    3    4

To get the right answer, I need transformation.

> t(apply(test.m, 1, rev))
[,1] [,2] [,3] [,4] [,5]
[1,]   17   13    9    5    1
[2,]   18   14   10    6    2
[3,]   19   15   11    7    3
[4,]   20   16   12    8    4

On the other hand, if I want to reverse the order of elements in column, t() is not necessary.

> apply(test.m, 2, rev)
[,1] [,2] [,3] [,4] [,5]
[1,]    4    8   12   16   20
[2,]    3    7   11   15   19
[3,]    2    6   10   14   18
[4,]    1    5    9   13   17
Categories
status

Some Vim commands related to buffers Show buffer…

Some Vim commands related to buffers

Show buffer list

:ls

Change to another buffer – any of these commands

:bn
:bp
:b 
Ctrl-6

Close current buffer

:bd
Categories
status

Rotate open windows in Vim Ctrl w r

Rotate open windows in Vim

Ctrl-w r