# Preface

## What is this?

This is a comparison of the grammar of ** R **, which is a language specialized for processing and analysis of statistical data, with the format of ** Java **, which is a general-purpose language. Please refer to it so that those who understand Java can use it when they want to write R.

## Required prerequisite knowledge (Java side)

・ Understand variable types (int, double, boolean, String, Object) ・ Understand multidimensional arrays ・ List and Map can be used (some sample code is used) ・ Extended for statement [Java 8] can be used (same as above)

If you say what you want to do with R, you will understand if this area is held down.

## Required prerequisite knowledge (R side)

・ Installed R ・ I know that there is no need for a semicolon at the end of the sentence. ・ Yaruki

# R-Java Opposite Dictionary

## General-purpose grammar

### Common to Java and R

・ ** Arithmetic operators other than division entanglement ** (+,-, *) -Comparison operators (<,>, <=,> =,! =) -Conditional logical operators(&&, ||), Logical operators(&, |),denial(!)

### Different

Java R
Substitution `n = 0` `n <- 0`
Boolean value `true, false` `TRUE, FALSE`
(uppercase letter)
division(Integer quotient) `a / b` `a %/% b`*1
Surplus `a % b` `a %% b`
Exponentiation `Math.pow(a, b)` `a ^ b`
Increment
Decrement
`++a` or `a++`
`--a` or `a--`
(`a <- a+1`)
not exist
Standard output `System.out.println("Hello, world!");` `print("Hello, world!")`
Standard input Scanner class etc. `readline("input :")`*2
Definition of constants `final int N = 0;` not exist
Comment out `//comment`
`/* comment */`
`#comment`*3

Annotation

• 1: In R, even if you divide integer types, it will be returned as a floating point type (double), so if you simply enter 10/4, for example, 2.5 will be returned.
• 2: Be careful when relying on autocomplete because there is a similar but completely different function called `readLines ()`. Also, if it is not console interactive, this description will be ignored and the subsequent processing will proceed assuming that an empty string has been entered. You can check if it is interactive with `print (interactive ())`.
• 3: When commenting out over multiple lines, it is necessary to put # in all lines.

## if, for, switch statement

#### `if_for_switch.java`

``````
if(statement){
System.out.println(statement is true);
}

for(int i=0;i<n;i++){
System.out.println("for loop " + i);
}

switch(num){
case 0:
System.out.println("switch num=0");
break;
case 1:
System.out.println("switch num=1");
break;
default:
System.out.println("switch default");
}
``````

#### `if_for_switch.R`

``````
if(statement){
print("statement is TRUE")
}

for(i in 1:n){
print(paste("for loop ", i-1))
}

switch(num,
"0" = {print("switch num=0")},
"1" = {print("switch num=1")},
{print("switch default")}
)
``````

Annotation As in the standard output in the R for statement, it is not possible to output a string and a numerical value connected by + (strictly, it is not recommended even in Java). Use the `paste ()` method. The {} in each statement of the R switch statement can be omitted if the content is only one statement.

## How to call each element of a two-dimensional array (R: matrix)

#### `matrix.java`

``````
int[][] a = new int[4][4];
``````

#### `matrix.R`

``````
a <- matrix(ncol=4, nrow=4)
``````

Both make almost the same appearance (there are differences such as `0` if the initial value is java and` NA` if it is R. In the case of R, the value taken by the contents of the matrix type is a numerical value. Not exclusively) How to refer to each element of this matrix is as follows.

Java R
1 element reference `a[2][3]` `a[3,4]`
1 element reference `a[3][0]` `a[4]`Or`a[4,1]`
1 can be omitted only at the beginning of the line
Line reference `a[1]` `a[2,]`
Column reference - `a[,2]`

## Eliminate the use of for statements (apply family)

If you don't use the for statement, it's like R for one person, but it doesn't explain much, so let's see what the apply family is doing in opposition to Java.

### Apply a function to each element of a multidimensional array

#### `apply.java`

``````
//Array generation and initialization from here
int[][] mtrx = new int[4][4];
for(int i=0;i<4;i++){
for(int j=0;j<4;i++){
mtrx[i][j] = i * 4 + j;
}
}
//So far
//Preparation of functions to be applied repeatedly from here
return i+1;
}
//So far
//From here, scan the array and apply the function
for(int i=0;i<4;i++){
for(int j=0;j<4;i++){
}
}
//So far
``````

A two-dimensional array containing numbers from 0 to 15 is generated, and 1 is added to each element (of course, to explain in comparison, this is a hair-raising ~ ~ redundant writing style. ).

#### `apply.R`

``````
#Matrix generation and initialization from here
a <- matrix(c(1:16), ncol=4, nrow=4, byrow=TRUE)
#So far
#Preparation of functions to be applied repeatedly from here
return(i+1)
}
#So far
#From here, scanning the matrix and applying the function
#So far
``````

It can be written very simply like this. I've created a function that runs repeatedly here, but you can use any of the available functions (for example, sqrt gives you a matrix with the square roots of all the elements).

Annotation -The `byrow` option of the`matrix ()`function is an option to arrange the source vector (`c (1:16) `in this example) to be a matrix in row units. The default is FALSE, so this code will generate a transposed version of the resulting matrix. -The second option (`c (1: 2)`) of the ʻapply ()` function specifies the scope. Do the same with 1 for "all rows" and 2 for "all columns". ** If you just want to write a for loop on a 2D array, you don't have to worry about it at all, so don't mess with it carelessly **. Specifically, the following two codes do roughly the same thing. R is really just this, not overwriting.

#### `apply2.java`

``````
int sum(int[] arr){  //A function that gives the sum of arrays. R comes standard
int sum = 0;
for(int i : arr){
sum += i;
}
return sum;
}

int[] sum_arr = new int[4];
for(int i=0;i<4;i++){
sum_arr[i] = sum(mtrx[i]);  //Sum for all lines()Apply function
}
``````

#### `apply2.R`

``````
apply(a, 1, sum)
``````

### Apply function to each element of HashMap (lapply)

First, it is necessary to understand that list type variables in R ** do not need to have the same type of values held inside **. In other words, it is like declaring that Java Map handles object types unless you declare anything in particular.

#### `lapply.java`

``````
Map<String, Object> a = new HashMap<>();
a.put("i", 123);
a.put("d", 4.56d);
a.put("b", false);
``````

#### `lapply.R`

``````
a <- list(i = 123, d = 4.56, b = FALSE)
``````

In this way, it is possible to have an integer type, a double precision floating point type, and a logical type at the same time. lapply performs processing on a list or matrix and ** returns the result as a list **.

#### `lapply2.java`

``````
Map<String, Object> a = new HashMap<>();
a.put("i", 123);
a.put("d", 4.56d);
a.put("b", false);

for(Object o : a.values()){
}    //Compile error!
``````

#### `lapply2.R`

``````
a <- list(i = 123, d = 4.56, b = FALSE)

``````

Since the argument of the add () function defined in apply.java is int type, the above code cannot be executed in Java. But in R there is no need for a type declaration and the variables are automatically matched to the largest type so you can execute this code (treated as FALSE = 0, TRUE = 1) and print: To.

``````> lapply(a, add)
\$i
[1] 124

\$d
[1] 5.56

\$b
[1] 1
``````

You can use lapply for 2D arrays, but it will be awkward unless you have to output as a list type. You can assume that it is such a function. For the 4x4 matrix generated by apply.java (apply.R), the following two lapply () output almost the same thing.

#### `lapply3.java`

``````
Map<String, Integer> lapply(int[][] arr){
Map<String, Integer> a = new HashMap<>();
for(int i=0;i<arr[0].length;i++){
for(int j=0;j<arr.length;j++){
a.put(String.valueOf(i * 4 + j + 1), add(arr[j][i]));
//arr[i][j]Not arr[j][i]Note that
//In R, scanning for 2D arrays is done column by column
}
}
return a;
}
``````

#### `lapply3.R`

``````
#apply.Add to all elements of a as in R()Apply, but
#The appearance of the output result is apply.It's totally different from R. Please run and check
``````

Annotation -In lapply3.java, `Map <String, Integer>` is used as the return type, but in reality, the number from 1 to 16 (cast to String type) is entered in Key, so it looks redundant. Since the R matrix can have names for row numbers and column numbers, such a description is used because the value entered in Key may not be a numerical value if a strict correspondence is attempted.

## String processing

Overall, R is not good at character string processing. If a large amount of character string processing is required for data preprocessing, consider using another language or using spreadsheet software first.

#### `str_concat.java`

``````
//Example 1
String str = "Hello" + " " + "world!";  //Not recommended as it consumes more memory

//Example 2
StringBuilder builder = new StringBuilder();
builder.append("Hello");
builder.append(" ");
builder.append("world!");
String str = builder.toString();
``````

#### `str_concat.R`

``````
str <- paste("Hello", " ", "world!")
``````