on bce304a help sign in
170 lines 4.3 kB history raw
1# take input from the user
2num = as.integer(readline(prompt="Enter a number: "))
3factorial = 1
4# check is the number is negative, positive or zero
5if(num < 0) {
6print("Sorry, factorial does not exist for negative numbers")
7} else if(num == 0) {
8print("The factorial of 0 is 1")
9} else {
10for(i in 1:num) {
11factorial = factorial * i
13print(paste("The factorial of", num ,"is",factorial))
16x <- 0
17if (x < 0) {
18print("Negative number")
19} else if (x > 0) {
20print("Positive number")
21} else
22print("Zero")
24x <- 1:5
25for (val in x) {
26if (val == 3){
27next
29print(val)
32x <- 1
33repeat {
34print(x)
35x = x+1
36if (x == 6){
37break
41`%divisible%` <- function(x,y)
43if (x%%y ==0) return (TRUE)
44else return (FALSE)
47switch("length", "color" = "red", "shape" = "square", "length" = 5)
48[1] 5
50recursive.factorial <- function(x) {
51if (x == 0) return (1)
52else return (x * recursive.factorial(x-1))
55pow <- function(x, y) {
56# function to print x raised to the power y
57result <- x^y
58print(paste(x,"raised to the power", y, "is", result))
61A <- read.table("x.data", sep=",",
62 col.names=c("year", "my1", "my2"))
63nrow(A) # Count the rows in A
65summary(A$year)
67A$newcol <- A$my1 + A$my2 # Makes a new
68newvar <- A$my1 - A$my2 # Makes a
69A$my1 <- NULL # Removes
70str(A)
71summary(A)
72library(Hmisc)
73contents(A)
74describe(A)
76set.seed(102) # This yields a good illustration.
77x <- sample(1:3, 15, replace=TRUE)
78education <- factor(x, labels=c("None", "School", "College"))
79x <- sample(1:2, 15, replace=TRUE)
80gender <- factor(x, labels=c("Male", "Female"))
81age <- runif(15, min=20,max=60)
83D <- data.frame(age, gender, education)
84rm(x,age,gender,education)
85print(D)
87# Table about education
88table(D$education)
90# Table about education and gender --
91table(D$gender, D$education)
92# Joint distribution of education and gender --
93table(D$gender, D$education)/nrow(D)
95# Add in the marginal distributions also
96addmargins(table(D$gender, D$education))
97addmargins(table(D$gender, D$education))/nrow(D)
99# Generate a good LaTeX table out of it --
100library(xtable)
101xtable(addmargins(table(D$gender, D$education))/nrow(D),
102 digits=c(0,2,2,2,2))
104by(D$age, D$gender, mean)
105by(D$age, D$gender, sd)
106by(D$age, D$gender, summary)
108a <- matrix(by(D$age, list(D$gender, D$education), mean), nrow=2)
109rownames(a) <- levels(D$gender)
110colnames(a) <- levels(D$education)
111print(a)
112print(xtable(a))
114dat <- read.csv(file = "files/dataset-2013-01.csv", header = TRUE)
115interim_object <- data.frame(rep(1:100, 10),
116 rep(101:200, 10),
117 rep(201:300, 10))
118object.size(interim_object)
119rm("interim_object")
120ls()
121rm(list = ls())
123vector1 <- c(5,9,3)
124vector2 <- c(10,11,12,13,14,15)
125array1 <- array(c(vector1,vector2),dim = c(3,3,2))
126vector3 <- c(9,1,0)
127vector4 <- c(6,0,11,3,14,1,2,6,9)
128array2 <- array(c(vector1,vector2),dim = c(3,3,2))
129matrix1 <- array1[,,2]
130matrix2 <- array2[,,2]
131result <- matrix1+matrix2
132print(result)
134column.names <- c("COL1","COL2","COL3")
135row.names <- c("ROW1","ROW2","ROW3")
136matrix.names <- c("Matrix1","Matrix2")
137result <- array(c(vector1,vector2),dim = c(3,3,2),dimnames = list(row.names,
138 column.names, matrix.names))
139print(result[3,,2])
140print(result[1,3,1])
141print(result[,,2])
143# Load the package required to read JSON files.
144library("rjson")
145result <- fromJSON(file = "input.json")
146print(result)
148x <- c(151, 174, 138, 186, 128, 136, 179, 163, 152, 131)
149y <- c(63, 81, 56, 91, 47, 57, 76, 72, 62, 48)
150relation <- lm(y~x)
151print(relation)
153relation <- lm(y~x)
154png(file = "linearregression.png")
155plot(y,x,col = "blue",main = "Height & Weight Regression",
156abline(lm(x~y)),cex = 1.3,pch = 16,xlab = "Weight in Kg",ylab = "Height in cm")
157dev.off()
159data <- c("East","West","East","North","North","East","West","West","West","East","North")
160print(data)
161print(is.factor(data))
162factor_data <- factor(data)
163print(factor_data)
164print(is.factor(factor_data))
166v <- c(7,12,28,3,41)
168# Give the chart file a name.
169png(file = "line_chart_label_colored.jpg")
170plot(v,type = "o", col = "red", xlab = "Month", ylab = "Rain fall", main = "Rain fall chart")