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