Rscript test.RBefore using the Rscript command, set the environment path to use the Rscript executable.
c scan mode print cat
> u = c(2, 4, 6) > v = c(3, 7, 9) > w = c(u, v, 11) > w [1] 2 4 6 3 7 9 11You can also create a vector with the scan function, which allows values to be entered at the keyboard:
> w = scan( ) 1: 2 4 6 3 7 6: 9 11 10: Read 10 items. > w [1] 2 4 6 3 7 9 11The scan function also reads values for a vector from an input file:
w = scan("values.txt")
w = c(32L, 17L, -345L)
mode(v) <- "character"
v = c(v, 376)or you can change the length directly:
length(v) <- 10
1:5 + 1:7Ans: The values of the shorter vector are padded with recycled values to make it the same length as the longer vector:
[1] 2 4 6 8 10 7 9This warning message is given if the length of the longer item is not a multiple of the length of the shorter item:
Warning message: In 1:5 + 1:7 : longer object length is not a multiple of shorter object length
M = matrix(c(4, 2, 7, 5), 4, 2) MAns: It produces this matrix. The values in the vector are cycled to fill out the six matrix entries in column major order.
[,1] [,2]
[1,] 4 4
[2,] 2 2
[3,] 7 7
[4,] 5 5
Because the matrix function argument byrow=TRUE is not supplied, the matrix values
are filled in by columns. This is probably because S (the predecessor of R) was
written Fortran, which is a column major language. Newer languages like C++, Java, and C#
are all row major languages.
n = 1
while(n < 100) {
cat(n, " ")
n = n * 2
}
cat("\n")
A for loop repeats the loop body for each item in a vector or list. For example:
for (n in c(4, 2, 6, 5)) {
cat(n ^ 2, " ")
}
Note: a for loop can often be written more simply by using R's vector capabilities:print c(4, 2, 6, 5)^2
> A <- matrix(c(3, 0, 2, 1, 1, 5), 2, 3, byrow=TRUE)
> A
[,1] [,2] [,3]
[1,] 3 0 2
[2,] 1 1 5
># Alternative way to create a matrix:
> B <- matrix(c(scan( ), 2, 3, byrow=TRUE)
1: 2 1 -3 0 -2 4
7:
Read 6 items
> B
[,1] [,2] [,3]
[1,] 2 1 -3
[2,] 0 -2 4
> A + B
[,1] [,2] [,3]
[1,] 5 1 -1
[2,] 1 -1 9
> C <- matrix(c(2, -1, 0, 1, 7, 3),
3, 2, byrow=TRUE)
> C
[,1] [,2]
[1,] 2 -1
[2,] 0 1
[3,] 7 3
> D = A %*% B
> D
[,1] [,2]
[1,] 20 3
[2,] 37 15
>
| m = |
|
monthlyPayment <- function(p, y, i) {
return((p * (i/1200) * (1 + i/1200)^(12*y)) /
((1 + i/1200)^(12*y) - 1))
}
monthlyPayment(100000, 15, 6)
What would be the monthly payment for a loan with principal $100,000, interest rate 6%, and
term of loan 15 years?
ones <- c('one', 'two', 'three', 'four', 'five', 'six',
'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve',
'thirteen', 'fourteen', 'fifteen', 'sixteen',
'seventeen', 'eighteen', 'nineteen')
tens <- c('ten', 'twenty', 'thirty', 'forty', 'fifty',
'sixty', 'seventy', 'eighty', 'ninety')
Ans:
numToWords <- function(n) {
if (n < 1) return("zero")
else if (n < 20)
return(ones[n])
else if (n < 99)
return(paste(tens[n %/% 10], ones[n %% 10]))
else if (n < 999)
return(paste(numToWords(n %/% 100), "hundred",
numToWords(n %% 100)))
else if (n < 999999)
return(paste(numToWords(n %/% 1000), "thousand",
numToWords(n %% 1000)))
}
> cars speed dist 1 4 2 2 4 10 3 7 4 ...
print(cars)
> library(help=datasets)