Articles on Trending Technologies

Technical articles with clear explanations and examples

Tutorix - AI Tutor

Difference between "fold" and "reduce" in Kotlin

Soumak De
Soumak De
Updated on 23-Nov-2021 1K+ Views

Kotlin is a cross-platform and statically typed general-purpose programming language. Kotlin provides many optional methods to traverse through the collections. fold() and reduce() are two different methods, both are helpful for traversing a collection. In this article, we will see how and when to use these two methods.Example – fold()If we want to traverse through a collection serially, then we can use fold().fold() takes one initial value and one operation to perform the operation on the initial value.There are different kinds of fold(), for example, foldRight() folds from right to left. By default, fold() will traverse from left to right.The ...

Read More

How to find the moving standard deviation in an R matrix?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 23-Nov-2021 506 Views

To find the moving standard deviation in a matrix is done in the same way as in a data frame, we just need to use the matrix object name in place of data frame name. Hence, we can make use of rollapply function of zoo package for this purpose.For example, if we have a matrix called M and we want to find the 2 moving standard deviations then we can use the below given command −rollapply(M,width=2,FUN=sd,fill=0,align="r")Example 1Following snippet creates a matrix −M1

Read More

Try-with-resources in Kotlin

Soumak De
Soumak De
Updated on 23-Nov-2021 813 Views

Kotlin is very efficient in managing the memory. Unlike Java, developers in Kotlin need not have to manage the memory explicitly. We do have different kinds of memory management techniques and Try-with-resource is one of them. In Kotlin, we have a function called 'use' which takes the burden of managing the resources automatically. This is a part of std library function provided by Kotlin.As per Kotlin documentation, use() is defined as a generic extension on all closeable types. The implementation looks like this −public inline fun T.use(block: (T) -> R): R {}In the above function, the definition block is the ...

Read More

How to round the summary output in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 23-Nov-2021 5K+ Views

To round the output of summary function in R, we can use digits argument while applying the summary function.For example, if we have a data frame called df then to find the summary statistics with two digits in the output we can use the below given command −summary(df, digits=2)Example 1Following snippet creates a dataframe −head(iris, 20) The following dataframe is created − Sepal.Length Sepal.Width Petal.Length Petal.Width Species 1    5.1       3.5         1.4         0.2         setosa 2    4.9       3.0         1.4 ...

Read More

How to find the row wise sum for n number of columns in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 23-Nov-2021 4K+ Views

To find the row wise sum of n number of columns can be found by using the rowSums function along with subsetting of the columns with single square brackets.For example, if we have a data frame called df that contains five columns and we want to find the row sums for last three columns then we can use the following command −df$Sum_3

Read More

How to extract the maximum value from named vector in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 23-Nov-2021 1K+ Views

To extract the maximum value from named vector in R, we can use which.max function.For example, if we have a vector called X which is a named vector then we can use the following command to find the maximum value in X.X[which.max(X)]Check out the below examples to understand how it works.Example 1Following snippet creates a vector −x1

Read More

Swift "if let" statement equivalent in Kotlin

Soumak De
Soumak De
Updated on 23-Nov-2021 2K+ Views

Swift "if let" is a conditional check operator to check whether a reference variable is NULL or not. This is a very useful technique to evaluate the unwrap optional value using swift language.In Kotlin, we can't use this operator directly. Instead, we will be using "let" and "run" to evaluate the same. In the following example, we will see how to use "let" and "run" using Kotlin library class.Example: Null Check using 'let' and 'run'In this example, we will check whether the value of a reference variable is NULL or not. If the value of the reference variable is NULL, ...

Read More

How to reduce the space between Y-axis value and ticks using ggplot2 in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 23-Nov-2021 5K+ Views

To reduce the space between axis value and ticks using ggplot2, we can use theme function of ggplot2 package with margin set to 0.For example, if we have a data frame called df that contains two columns say x and y then the scatterplot between x and y with reduced space between Y-axis value and ticks can be created by using the following command −ggplot(df,aes(x,y))+geom_point()+theme(axis.text.y=element_text(margin=margin(r=0)))ExampleFollowing snippet creates a sample data frame −x

Read More

How to implement Builder pattern in Kotlin?

Soumak De
Soumak De
Updated on 23-Nov-2021 993 Views

In object-oriented programming, the creation of objects is very easy via constructors when the object definitions are simple, but sometimes, it may so happen that the constructors need more variables or functions in order to initialize an object. In such cases, the "builder pattern" comes into picture which helps programmers to create small units of different objects and create a final object.A builder pattern provides an API to construct an object step- by-step.Builder patterns are particularly useful when objects need to be created dynamically.Note that it is not recommended to use builder patterns in Kotlin because we can get the ...

Read More

How to display fraction in base R plot?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 23-Nov-2021 2K+ Views

To display fraction in base R plot, we can use frac function in text function.For example, if we create a plot of vectors called X and Y with display of over form fraction as X over Y at X=2 and Y= 2 then we can use the following code −text(2, 2, expression(Output==frac(X, Y)))Check out the below examples to understand how it works.Example 1To display fraction in base R plot, use the code given below −plot(1:5, type="n") text(2, 4, expression(Output==frac(x, y)))OutputIf you execute the above given code, it generates the following output −Example 2To display fraction in base R plot, use ...

Read More
Showing 47091–47100 of 61,298 articles
Advertisements