Manually entering and manipulating data

Perhaps the most convenient way to enter data is to use an editor or spreadsheet program and then copy the data into the clipboard. Then use the read.clipboard() function (found in the psych package).

The second most convenient way is to read the data from a file. Note that comma separated files (csv) can be used to read in fields that have embedded spaces. See ?read for more details.

Although it is probably more convenient to enter data from the clipboard or a data file, it is possible to enter small data sets directly into R by using the combine() and repeat functions. (Abbreviated c() ). Thus
x = c(10,15,18, 20) will form a vector (x) with 4 elements, and
x=rep(c(1,2,4,8),2) will form an 8 item vector

Consider the differences between c(), cbind(), and rbind() for combining two vectors. c() adds them 'head to tail' while cbind() combines them into a matrix form, column wise, rbind() adds them rowwise. The repeat command r(x,n) will do an operation x n times.



x=c(1,2,4,8)                               #create a data vector with specified elements
x                                          #show the result


x1=c(10:20)                                  #creat a data vector with elements 10-20
x1                                           #show it 


n=10                                       #specify the number of cases
x=c(rnorm(n))                              #create a n item vector of random normal deviates
y=c(rnorm(n))+n                           #create another n item vector of random deviates, but add n to all the scores
vect=c(x,y)                                #combine them into one vector of length 2n
round(vect,2)                              #display the vector, rounded to two decimal places

matc=cbind(x,y)                             #combine them into a n x 2 matrix
                                           #cbind combines column wise
                                           #rbind combines row wise
matr=rbind(x,y)                                           
round(matr,2)                              #display the rowwise matrix to 2 decimals
round(matc,3)                              #display the column wise matrix to 3 declmals
 



x2=c(1:4)                                   #create a 4 item vector with values 1 - 4
y2=c(1:12)                                  #create a 12 item vector with values 1-12
z=rep(c(11:13),4)                            #create a data vector with elements 11-13 repeated 4 times
z                                         #show this vector
vect=c(x2,y2,z)                            #find the 28 item vector of the cominbed x and y and z
matc=cbind(x2,y2,z)                         #create a 12*3 (column) matrix with repeated values of x
matr=rbind(x2,y2,z)                         #create a 3*12 (row) matrix with repeated values of x
vect                                       #show the vector
matr                                       #show the matrix formed row wise
matc                                       #show the matrix formed column wise
matc[5,2]                                   #display the 5th row and the 2nd column
matc[7,]                                    #display the 7rd row
matc[,2]                                    #display the 2nd column       
matc[2,]                                    #display the 2nd row

Produces this output
 
> x=c(1,2,4,8)                               #create a data vector with specified elements
> x                                          #show the result
[1] 1 2 4 8
> 
> 
> x1=c(10:20)                                  #creat a data vector with elements 10-20
> x1                                           #show it 
 [1] 10 11 12 13 14 15 16 17 18 19 20
> 
> 
> n=10                                       #specify the number of cases
> x=c(rnorm(n))                              #create a n item vector of random normal deviates
> y=c(rnorm(n))+n                           #create another n item vector of random deviates, but add n to all the scores
> vect=c(x,y)                                #combine them into one vector of length 2n
> round(vect,2)                              #display the vector, rounded to two decimal places
 [1] -0.10  0.17  0.08 -0.03  0.36  0.92  0.66  0.00  0.46 -0.71  8.94 10.02 10.29 10.35  8.27  8.16 10.11  8.47 10.51  9.11
> 
> matc=cbind(x,y)                             #combine them into a n x 2 matrix
>                                            #cbind combines column wise
>                                            #rbind combines row wise
> matr=rbind(x,y)                                           
> round(matr,2)                              #display the rowwise matrix to 2 decimals
   [,1]  [,2]  [,3]  [,4] [,5] [,6]  [,7] [,8]  [,9] [,10]
x -0.10  0.17  0.08 -0.03 0.36 0.92  0.66 0.00  0.46 -0.71
y  8.94 10.02 10.29 10.35 8.27 8.16 10.11 8.47 10.51  9.11
> round(matc,3)                              #display the column wise matrix to 3 declmals
           x      y
 [1,] -0.101  8.936
 [2,]  0.170 10.023
 [3,]  0.082 10.288
 [4,] -0.030 10.351
 [5,]  0.356  8.269
 [6,]  0.925  8.164
 [7,]  0.662 10.108
 [8,]  0.005  8.473
 [9,]  0.464 10.514
[10,] -0.710  9.114
>  
> 
> 
> 
> x2=c(1:4)                                   #create a 4 item vector with values 1 - 4
> y2=c(1:12)                                  #create a 12 item vector with values 1-12
> z=rep(c(11:13),4)                            #create a data vector with elements 11-13 repeated 4 times
> z                                         #show this vector
 [1] 11 12 13 11 12 13 11 12 13 11 12 13
> vect=c(x2,y2,z)                            #find the 28 item vector of the cominbed x and y and z
> matc=cbind(x2,y2,z)                         #create a 12*3 (column) matrix with repeated values of x
> matr=rbind(x2,y2,z)                         #create a 3*12 (row) matrix with repeated values of x
> vect                                       #show the vector
 [1]  1  2  3  4  1  2  3  4  5  6  7  8  9 10 11 12 11 12 13 11 12 13 11 12 13 11 12 13
> matr                                       #show the matrix formed row wise
   [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
x2    1    2    3    4    1    2    3    4    1     2     3     4
y2    1    2    3    4    5    6    7    8    9    10    11    12
z    11   12   13   11   12   13   11   12   13    11    12    13
> matc                                       #show the matrix formed column wise
      x2 y2  z
 [1,]  1  1 11
 [2,]  2  2 12
 [3,]  3  3 13
 [4,]  4  4 11
 [5,]  1  5 12
 [6,]  2  6 13
 [7,]  3  7 11
 [8,]  4  8 12
 [9,]  1  9 13
[10,]  2 10 11
[11,]  3 11 12
[12,]  4 12 13
> matc[5,2]                                   #display the 5th row and the 2nd column
[1] 5
> matc[7,]                                    #display the 7rd row
x2 y2  z 
 3  7 11 
> matc[,2]                                    #display the 2nd column       
 [1]  1  2  3  4  5  6  7  8  9 10 11 12
> matc[2,]                                    #display the 2nd row
x2 y2  z 
 2  2 12 
> 
 

part of a short guide to R
Version of November 25, 2005
William Revelle
Department of Psychology
Northwestern University