R: Entering Data
1. Introduction
There are a variety of ways to get data into R and Rweb, but if your dataset is small, manually entering the data is rather straightforward using the c() function. It's easiest just to think that c stands for combine.
Example 1: Enter the following dataset into R, and label the data my.data: {11, 14, 22, 15}
R CODE
There are a variety of ways to get data into R and Rweb, but if your dataset is small, manually entering the data is rather straightforward using the c() function. It's easiest just to think that c stands for combine.
Example 1: Enter the following dataset into R, and label the data my.data: {11, 14, 22, 15}
R CODE
my.data=c(11,14,22,15) #this code will create your dataset and assign it the variable name my.data
my.data #If you just submit the dataset above, nothing will be output in R, if you want to know what a variable holds, you must ask for it by name
R OUTPUT
[1] 11 14 22 15
2. Using ":"
Example 2: Create a list of numbers 1 through 5, assign the name stats.data to your data.
R CODE
Example 2: Create a list of numbers 1 through 5, assign the name stats.data to your data.
R CODE
stats.data=1:5 #this code will create your sequence of data from 1 to 5, by increments of 1
stats.data # if you wish to view the data, don't forget to ask for the data by name
R OUTPUT
[1] 1 2 3 4 5
Example 3: Create a list of numbers 5 through -6, assign the name decreasing.data to your data.
R CODE
R CODE
decreasing.data=5:-6 #this code will create your sequence of data from 5 to -6, by increments of 1
decreasing.data # if you wish to view the data, don't forget to ask for the data by name
R OUTPUT
[1] 5 4 3 2 1 0 -1 -2 -3 -4 -5 -6
3. Using seq() Function
The seq(from, to, by) function creates a sequence of numbers between any two numbers incremented by a specific number.
Example 4: Create a sequence of numbers from 1 through 3, with increments of 0.5, save as z.
R CODE
The seq(from, to, by) function creates a sequence of numbers between any two numbers incremented by a specific number.
Example 4: Create a sequence of numbers from 1 through 3, with increments of 0.5, save as z.
R CODE
z=seq(1,3,0.5) # seq(low.number, high.number, increment.size)
z
R OUTPUT
[1] 1.0 1.5 2.0 2.5 3.0
The seq(from, to, length.out) function creates a sequence of numbers with a specific length between any two numbers.
Example 4: Create a sequence of numbers with length 6 from 0 through 12.
R CODE
Example 4: Create a sequence of numbers with length 6 from 0 through 12.
R CODE
seq(0, 12, length.out=6)
R OUTPUT
[1] 0.0 2.4 4.8 7.2 9.6 12.0