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