LoginSignup
0
0

More than 5 years have passed since last update.

R言語使い方を調べたのでメモしておく場所

Last updated at Posted at 2016-02-26

introduction

Rちらうら

初期化

?vector

X <- vector(mode="character", length=10)
[1] "" "" "" "" "" "" "" "" "" ""

ライブラリ

一覧

library()

文字列操作

文字連結

paste("hello", "world", "!!!", sep = " ")
[1] "hello world !!!"

ダブルクォートを消す

my.list <- data.frame(y1=c(1,2,3),y2=c(4,5,6),y3=c(8,9,10))

colnames(my.list,do.NULL = TRUE, prefix = "col")[1]
[1] "y1"

cat(colnames(my.list,do.NULL = TRUE, prefix = "col")[1])
y1

cast

?as.numeric
?as.integer
?as.complex
?as.character
?as.logical
?as.factor
?as.ordered

ファイル

BOM付きUTF-8を読む

source("youer.txt", encoding="UTF-8-BOM") 

行列を指定する

my.list <- data.frame(y1=c(1,2,3),y2=c(4,5,6),y3=c(8,9,10))
my.list[2,3]

[1] 9

function

普通のやつ

aaa <- function(x) x + 1
aaa(5)
[1] 6

即時関数

(function(x) {return (paste("hello",x, sep = " "))})("world")
[1] "hello world"

map

my.list <- c(1,2,3)
(function(x) {return (x + 100)})(my.list)

[1] 101 102 103

カラム名を取得する

my.list <- data.frame(y1=c(1,2,3),y2=c(4,5,6),y3=c(8,9,10))
colnames(my.list)
[1] "y1" "y2" "y3"

help(colnames)

検索

いいサイトあった
http://d.hatena.ne.jp/a_bicky/20110529/1306667230

Select

列を選ぶ

my.list <- data.frame(y1=c(1,2,3),y2=c(4,5,6),y3=c(8,9,10))
my.list[c("y1", "y3")]

  y1 y3
1  1  8
2  2  9
3  3 10

Where

検索する

my.list <- data.frame(y1=c(1,2,3),y2=c(4,5,6))
subset(my.list, y1 == 1)

  y1 y2
1  1  4

# AND
a <- as.numeric(my.list[1,]["y1"])
b <- as.numeric(my.list[1,]["y2"])
my.list[my.list$y1 == a & my.list$y2 == b,]

  y1 y2
1  1  4

一番左のカラムをselectする

my.list <- data.frame(y1=c(1,2,3),y2=c(4,5,6))
my.list[my.list[1] == 2,]

  y1 y2 y3
2  2  5  9

group by

# max
my.list <- data.frame(y1=c(1,2,3,1,2,3),y2=c(4,5,6,7,8,9))
aggregate(my.list$y2, by = list(my.list$y1), max)

  Group.1 x
1       1 7
2       2 8
3       3 9

上澄みを何行かとる

my.list <- data.frame(y1=c(1,2,3),y2=c(4,5,6))
head(my.list, n = 2)
  y1 y2
1  1  4
2  2  5
tail(my.list, n = 2)
  y1 y2
2  2  5
3  3  6

csvの上澄みを何行か取る

install.packages("R.utils")
l2keep <- 10
nL <- countLines("your.csv")
df <- read.csv("your.csv", header=FALSE, skip=nL-l2keep)

配列に追加

a <- vector(, 0)
a <- c(a, 1)
a <- c(a, 2)
a <- c(a, 3)
a <- c(a, 4)

a
[1] 1 2 3 4

配列同士を計算

a <- c(1,2,3)
b <- c(1,2,3)
a * b
[1] 1 4 9

a/ b
[1] 1 1 1

最後にすべてのオブジェクトをREMOVE

(カチャカチャカチャッ・・・)

rm(list=ls())

(ッターン!)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0