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.
・ 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.
・ Installed R ・ I know that there is no need for a semicolon at the end of the sentence. ・ Yaruki
・ ** Arithmetic operators other than division entanglement ** (+,-, *) -Comparison operators (<,>, <=,> =,! =) -Conditional logical operators(&&, ||), Logical operators(&, |),denial(!)
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
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 ())
.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.
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] Ora[4,1] 1 can be omitted only at the beginning of the line |
Line reference | a[1] |
a[2,] |
Column reference | - | a[,2] |
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.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
int add(int i){
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++){
mtrx[i][j] = add(mtrx[i][j]);
}
}
//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
add <- function(i){
return(i+1)
}
#So far
#From here, scanning the matrix and applying the function
apply(a, c(1,2), add)
#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 thematrix ()
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)
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()){
add(o);
} //Compile error!
lapply2.R
a <- list(i = 123, d = 4.56, b = FALSE)
lapply(a, add) #add()The function is apply.Diverted from R
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
lapply(a, add)
#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.
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!")
Java -Java version: 1.8.0_231 -IDE: Intellij IDEA Community Edition 2019.2
R -R version: R x64 3.6.0 -IDE: R Studio 1.2.1335
Please refer to it if you cannot execute it well.
Recommended Posts