Showing posts with label R. Show all posts

make group (aggregate) data set in R

Suppose you have a data set called sort2013
And you want to calculate the group/aggregate of each Head_age

> sort2013
  Head_age P2_age P3_age P4_age P5_age P6_age P7_age P8_age
1       84     75      0      0      0      0      0      0
2       84     75      0      0      0      0      0      0
3       84     75      0      0      0      0      0      0
4       84     75      0      0      0      0      0      0
5       84     75      0      0      0      0      0      0
6       85     76      0      0      0      0      0      0
Etc...

Do the aggregate or grouping the data frame with aggregate()
> group2013 = aggregate(. ~ Head_age, sort2013, mean)
#aggregate or group of .(all column) ~(by) Head_age, in sort2013 data frame, and calculate the mean of all column. Then input into group2013 data frame

The result would be

> group2013
  Head_age   P2_age   P3_age   P4_age P5_age P6_age P7_age P8_age
1       16  0.00000 0.000000 0.000000      0      0      0      0
2       17  5.00000 0.000000 0.000000      0      0      0      0
3       18 13.44444 0.000000 0.000000      0      0      0      0
4       19 22.38095 7.000000 0.000000      0      0      0      0
5       20 17.45455 0.000000 0.000000      0      0      0      0
6       21 42.60000 6.666667 8.466667      0      0      0      0
Etc...


The result would be automatically sorted as you can see above

sort data in each row in R data set

Suppose we have this kind of data A
And want to sort from the maximum until the minimum value each row

> A
    [1]      [2]          [3]          [4]
[1] 3       7             9              5
[2] 7       9            11             3
[3] 5       3            7               8

We want to sort A by row and resulting like this
    [1]      [2]          [3]          [4]
[1] 3       5             7             9
[2] 3       7             9            11
Etc....

We can sort by row by doing this
>Asorted = t(apply(A,1,sort))  
#sort the data A each row and input to Asorted data

apply(A,1,sort) means apply for A data set, in 1 (row), and sort them
*1: row, 2: column and (1:2): both of row and column

Yet it will be like this. It will be sorted from the minimum value to the maximum value in each row  (reverse)

    [1]      [2]          [3]          [4]
[1] 3       5             7             9
[2] 3       7             9            11
Etc....

What we need to do next is just reverse it by this command
>Asorted = Asorted[ ,ncol(Asorted):1]    
#reverse sequence in column index, and input it (rewrite) to Asorted data

There you go, the result would be like this
>Asorted

    [1]      [2]          [3]          [4]
[1] 9       7             5            3
[2] 11     9             7            3
Etc....

change R language



As you know that R is a super powerful software
The story was started since I installed R software like a year ago and never use it
Then I started to use it for some period and I realized something, "the language is in Japanese!" Well, I am okay with Japanese, but what if I got result and need to put it in the paper?!
After searching, I got some inspiration in my dream <<== a big liar

皆さんご存知だと思いますが、Rっというものはスーパーパワーフールソフトでございま~す。
実は一年前にインストールしましたが、全然使わなくてですね…
それで必要なので使い始めましたが、”えぇ?!これ日本語じゃないですか!”まぁ、日本語はまあまあなんとなく読めますし、問題ないかと思っていたら…Rの結果を論文に載せようとしたらまずいですね。
それである時に、夢でインスピレーションが来ました <<== 嘘に決まってるでしょう。
とにかくこういうやり方です。

This is the Japanese screen shot



Well, what you need to do is close the R, go to your desktop, right click on it, choose property and put language=en in the property link and click OK
en is for English, it means Italian and so on.

Rを閉じて、デスクトップにRアイコンを見つけて、右クリック、プロパティ、リンク先のところに「language=en」と入れておいてください。それで、OKをクリックしてください。
en=英語、it=イタリア語等

as you can see there⇩⇩⇩



Open your R
voila! 

Rを立ち上げてくください
じゃじゃじゃ~ン

Your R is in English man!
 
 でございます。

Okay then, I need to back to my reality, work!
あぁ!仕事に戻らないといけないですね。

Posted by Picasa