石田修二ホームページ > 統計 >    (サイト内検索

Rの練習

Type the string "Arr, matey!". (Don't forget the quotes!)

> "Arrm matey!"
[1] "Arrm matey!"

Now try multiplying 6 times 7 (* is the multiplication operator).

6 * 7

Variables(変数)

As in other programming languages, you can store values into a variable to access it later. Type x <- 42 to store a value in x.

代入は x <- 42 または x = 42 のように書きます。

x can now be used in expressions in place of the original result. Try dividing x by 2 (/ is the division operator).

> x/2
[1] 21

Functions(関数)

合計は SUM を使います。

> sum(1, 3, 5)
[1] 9

次のデータは8匹のペットハムスターの体長です。

7.6, 8.2, 9.6, 7.1, 10.3, 8.5, 9.3, 10.6

この体長の合計を求めなさい。(室淳子,石村貞夫『平均・分散・標準偏差』東京図書,p4)

7.6 + 8.2 + 9.6 + 7.1 + 10.3 + 8.5 + 9.3 + 10.6で求めることができます。しかし,ここでは体長という変数を用意し,SUM関数で処理してみましょう。

> 体長 = c(7.6, 8.2, 9.6, 7.1, 10.3, 8.5, 9.3, 10.6)
> sum(体長)
[1] 71.2

したがって,8匹のペットハムスターの体長の合計は 71.2 cm です。

> rep("Yo ho!", times =3)
[1] "Yo ho!" "Yo ho!" "Yo ho!"

Try calling the sqrt function to get the square root of 16.

> sqrt(16)
[1] 4

Help(ヘルプ)

help(functionname) brings up help for the given function. Try displaying help for the sum function:

> help(sum)
sum                    package:base                    R Documentation

Sum of Vector Elements

Description:

     'sum' returns the sum of all the values present in its arguments.

Usage:

     sum(..., na.rm = FALSE)
...

example(functionname) brings up examples of usage for the given function. Try displaying examples for the min function:

> example(min)

min> require(stats); require(graphics)

min>  min(5:1, pi) #-> one number
[1] 1

min> pmin(5:1, pi) #->  5  numbers
[1] 3.141593 3.141593 3.000000 2.000000 1.000000

...

Files(ファイル管理)

We've stored a couple sample scripts for you. You can list the files in the current directory from within R, by calling the list.files function. Try it now:

> list.files()
[1] "bottle1.R" "bottle2.R"

To run a script, pass a string with its name to the source function. Try running the "bottle1.R" script:

> source("bottle1.R")
[1] "This be a message in a bottle1.R!"

Vectors(要素)

A vector's values can be numbers, strings, logical values, or any other type, as long as they're all the same type. Try creating a vector of numbers, like this:

> c(4,7,9)
[1] 4 7 9

The c function (c is short for Combine) creates a new vector by combining a list of values.

Now try creating a vector with strings:

> c('a','b','c')
[1] "a" "b" "c"

Vectors cannot hold values with different modes (types). Try mixing modes and see what happens:

> c(1,TRUE,"three")
[1] "1"     "TRUE"  "three"

All the values were converted to a single mode (characters) so that the vector can hold them all.

Sequence Vectors

If you need a vector with a sequence of numbers you can create it with start:end notation. Let's make a vector with values from 5 through 9:

> 5:9
[1] 5 6 7 8 9

A more versatile way to make sequences is to call the seq function. Let's do the same thing with seq:

> seq(5,9)
[1] 5 6 7 8 9

seq also allows you to use increments other than 1. Try it with steps of 0.5:

> seq(5,9,0.5)
[1] 5.0 5.5 6.0 6.5 7.0 7.5 8.0 8.5 9.0

Now try making a vector with integers from 9 down to 5:

> 9:5
[1] 9 8 7 6 5

Plotting One Vector(一要素のグラフ)

The barplot function draws a bar chart with a vector's values. We'll make a new vector for you, and store it in the vesselsSunk variable.

Now try passing the vector to the barplot function:

> vesselsSunk <- c(4, 5, 1)
> barplot(vesselsSunk)

If you assign names to the vector's values, R will use those names as labels on the bar plot. Let's use the names assignment function again:

> names(vesselsSunk) <- c("England", "France", "Norway")

Now, if you call barplot with the vector again, you'll see the labels:

> barplot(vesselsSunk)

Now, try calling barplot on a vector of integers ranging from 1 through 100:

> barplot(1:100)

リンクはご自由にどうぞ。

【石田修二トップページ】  【統計】

Last modified: 2016-11-12 14:23:08