Articles on Trending Technologies

Technical articles with clear explanations and examples

How to create a random sample of values between 0 and 1 in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 06-Nov-2020 4K+ Views

The continuous uniform distribution can take values between 0 and 1 in R if the range is not defined. To create a random sample of continuous uniform distribution we can use runif function, if we will not pass the minimum and maximum values the default will be 0 and 1 and we can also use different range of values.Examplesrunif(5) [1] 0.8667731 0.7109824 0.4466423 0.1644701 0.5611908 runif(10) [1] 0.5923782 0.8793613 0.6912947 0.2963916 0.6076762 0.7683766 0.1143595 [8] 0.4782710 0.1143383 0.4540217 runif(50) [1] 0.841674685 0.325249762 0.640847906 0.203868249 0.495230429 0.897175830 [7] 0.744447459 0.490173680 0.254711280 0.144844443 0.867749180 0.004405166 [13] 0.539785687 0.739637398 0.062214554 0.648021581 0.768686809 0.305543906 [19] 0.757496413 0.527085302 0.633331579 0.700118363 0.857950259 0.929350618 [25] 0.167015719 0.775870043 0.430343200 0.528408273 0.600575697 0.612206968 [31] 0.065904791 0.061135682 0.082027863 0.193586800 0.013956337 0.156875620 [37] 0.837501421 0.971202297 0.930835689 0.292126061 0.599263353 0.826630821 [43] 0.509235736 0.741715013 0.224485511 0.113099235 0.395143355 0.375654137 [49] 0.973050494 0.107550270 round(runif(50),2) [1] 0.51 0.70 0.90 0.45 0.41 0.74 0.31 0.40 0.10 0.05 0.18 0.05 0.63 0.34 0.57 [16] 0.06 0.73 0.37 0.79 0.85 0.82 0.41 0.32 0.34 0.37 0.14 0.21 0.11 0.43 0.86 [31] 0.83 0.09 0.88 0.04 0.62 0.64 0.15 0.75 0.78 0.16 0.67 0.97 0.79 0.64 0.56 [46] 0.40 0.07 0.69 0.82 0.63 round(runif(50),4) [1] 0.2951 0.2916 0.9049 0.2669 0.7613 0.2080 0.4739 0.1110 0.6155 0.5429 [11] 0.4490 0.2941 0.8262 0.7719 0.7896 0.7634 0.6260 0.7812 0.7600 0.6852 [21] 0.9142 0.0165 0.2324 0.0821 0.0814 0.4009 0.3315 0.8843 0.9684 0.1966 [31] 0.4841 0.5795 0.7898 0.1865 0.6929 0.8599 0.0492 0.8275 0.7431 0.3122 [41] 0.8480 0.3327 0.4872 0.0503 0.1887 0.0296 0.6011 0.1162 0.7776 0.6874 round(runif(50),5) [1] 0.40368 0.33585 0.03557 0.06047 0.95041 0.18260 0.70011 0.75148 0.12414 [10] 0.01310 0.42343 0.05846 0.21341 0.05454 0.77823 0.66151 0.61406 0.59459 [19] 0.50299 0.96780 0.43033 0.64652 0.39697 0.05897 0.47169 0.79828 0.74154 [28] 0.56074 0.97303 0.35301 0.36110 0.67452 0.14553 0.45195 0.05780 0.90489 [37] 0.96745 0.28014 0.02089 0.77789 0.04797 0.03550 0.40495 0.08924 0.59908 [46] 0.89074 0.48498 0.47335 0.59422 0.00719 round(runif(100),2) [1] 0.10 0.06 0.51 0.89 0.80 0.68 0.97 0.58 0.60 0.79 0.96 0.48 0.29 0.16 0.42 [16] 0.35 0.46 0.18 0.46 0.34 0.48 0.35 0.72 0.10 0.50 0.93 0.30 0.54 0.85 0.19 [31] 0.12 0.10 0.47 0.66 0.43 0.09 0.44 0.86 0.99 0.31 0.10 0.61 0.20 0.15 0.02 [46] 0.25 0.33 0.75 0.98 0.23 0.21 0.70 0.42 0.24 0.87 0.84 0.99 0.06 0.75 0.48 [61] 0.84 0.35 0.48 0.62 0.40 0.25 0.07 0.08 0.75 0.40 0.83 0.95 0.00 0.87 0.27 [76] 0.53 0.21 0.41 0.28 0.83 0.90 0.26 0.50 0.19 0.70 0.93 0.24 0.45 0.33 0.84 [91] 0.15 0.81 0.62 0.17 0.08 0.76 0.74 0.11 0.20 0.49 round(runif(150),1) [1] 0.6 0.3 0.3 0.3 0.9 0.7 0.1 0.1 0.1 0.9 0.4 0.6 1.0 0.0 0.4 1.0 0.1 1.0 [19] 0.8 0.0 0.9 0.9 0.7 0.7 0.7 0.7 0.3 0.7 0.1 0.1 0.9 0.0 0.1 1.0 0.9 1.0 [37] 0.9 0.6 0.0 0.4 0.4 1.0 0.2 0.4 0.2 0.8 0.3 0.9 0.8 0.6 0.3 0.3 0.4 0.7 [55] 0.2 0.9 1.0 0.9 0.8 0.7 0.9 1.0 0.5 0.8 0.6 0.8 0.6 0.8 0.3 0.3 1.0 0.6 [73] 0.9 0.3 0.0 1.0 0.5 0.6 0.7 0.7 0.6 0.3 0.4 0.0 0.3 0.1 0.6 0.2 0.1 0.7 [91] 0.9 0.8 0.3 0.2 0.5 0.6 0.6 0.1 0.0 0.9 0.4 0.6 0.3 0.2 0.9 0.6 0.0 0.2 [109] 0.3 0.3 0.3 0.7 0.4 0.8 0.5 0.9 0.6 0.5 0.3 1.0 0.6 0.7 0.9 0.1 0.8 1.0 [127] 0.3 1.0 0.2 0.9 0.2 0.3 0.5 0.4 0.1 0.6 0.6 0.0 0.3 0.3 0.0 0.3 0.3 1.0 [145] 0.6 0.5 0.1 0.7 0.6 0.4 round(runif(75),1) [1] 0.7 0.3 0.7 0.9 0.8 0.1 0.4 0.2 0.5 0.4 0.1 0.7 0.1 0.6 1.0 0.3 0.4 0.7 0.2 [20] 0.2 0.3 0.4 0.4 0.0 0.1 0.2 0.3 0.5 0.1 1.0 0.3 0.5 0.3 0.7 0.1 0.6 0.6 0.6 [39] 0.5 0.7 0.5 0.8 0.1 1.0 0.7 0.4 0.6 0.1 0.5 0.5 0.9 0.3 0.8 0.9 0.3 0.9 0.7 [58] 0.6 0.8 0.4 0.4 0.7 0.4 0.1 0.2 0.6 0.6 0.9 0.3 0.6 0.5 0.9 0.2 0.3 0.2 round(runif(75),3) [1] 0.712 0.355 0.130 0.768 0.134 0.681 0.273 0.663 0.849 0.851 0.842 0.430 [13] 0.371 0.903 0.148 0.879 0.812 0.330 0.567 0.646 0.199 0.159 0.056 0.448 [25] 0.637 0.204 0.101 0.389 0.797 0.030 0.021 0.167 0.440 0.359 0.670 0.435 [37] 0.807 0.669 0.738 0.546 0.535 0.969 0.055 0.201 0.436 0.336 0.841 0.548 [49] 0.901 0.850 0.369 0.770 0.678 0.922 0.252 0.132 0.635 0.544 0.291 0.715 [61] 0.601 0.399 0.585 0.161 0.423 0.244 0.451 0.397 0.951 0.382 0.123 0.959 [73] 0.252 0.330 0.238

Read More

How to set the alignment of labels in horizontal bar plot to left in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 06-Nov-2020 1K+ Views

When we create a horizontal bar plot using ggplot2 package, the labels of the categorical variable are aligned to the right-side of the axis and if the size of these labels are different then it looks a little ambiguous. Therefore, we might want to set the alignment of the labels to left-side and this can be done by using theme function of ggplot2 package.ExampleConsider the below data frame:> df dfOutput    x y 1 India  14 2 UK     15 3 Russia 12 4 United States of America 18Loading ggplot2 package and creating a horizontal ...

Read More

How to check the difference between plot generation time in base R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 06-Nov-2020 146 Views

One of the mostly used time measurement function in R is microbenchmark function of microbenchmark package. We can pass the function to create the plot inside microbenchmark function and this will result in the processing time for each of the plots then a comparison can be done for the difference.Example1Loading microbenchmark package:> library(microbenchmark)Finding the plot generation time:> x1 x2 x3 X XUnit: milliseconds expr min lq mean median uq max neval plot(x1) 12.7488 14.88815 15.65040 15.2515 15.90765 23.9348 100 plot(x2) 20.9810 21.67780 23.92976 22.2116 23.29665 137.2474 100 plot(x3) 93.6965 95.03440 96.67086 95.6717 97.12290 125.3670 100Plots:Example> plot(x1)Output:Example> plot(x2)Output:Example> plot(x3)Output:

Read More

How to generate passwords with varying lengths in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 06-Nov-2020 271 Views

To generate passwords, we can use stri_rand_strings function of stringi package. If we want to have passwords of varying length then we need to create the passwords using the particular size separately. For example, for a size or length of the password equals to 8, we can use the argument length in the stri_rand_strings function.Loading stringi package:> library(stringi)Example1> stri_rand_strings(n=5, length=8, pattern="[0-9a-zA-Z]") [1] "YkIEDYQz" "t42JCzYO" "rOE9YN8U" "2lu9AonY" "6lDUxScX"Example2> stri_rand_strings(n=20, length=8, pattern="[0-9a-zA-Z]") [1] "glH3ysoX" "X0Sgvg3F" "P3YOePTa" "45GOb2hA" "tLCwszus" "CerCi1ks" [7] "UtFwzrSc" "pG8AJCQX" "NTCdMRHj" "5thI1wKb" "Ic8Rol1Y" "JakWa1Wd" [13] "9AfeXo7T" "SFJVn9XV" "lIRhLbJ9" "DNFyAbkJ" "jV4jJRZk" "IthkzfEU" [19] "talj9nBq" "Nak9Tidh"Example3> ...

Read More

How to create EditText accepts Alphabets only in Kotlin?

Azhar
Azhar
Updated on 05-Nov-2020 328 Views

This example demonstrates how to create EditText accepts Alphabets only in Kotlin.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.         Step 3 − Add the following code to src/MainActivity.kt        

Read More

How to add a TextView to a LinearLayout dynamically in Android using Kotlin?

Azhar
Azhar
Updated on 05-Nov-2020 2K+ Views

This example demonstrates how to add a TextView to a LinearLayout dynamically in Android using Kotlin.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml. Step 3 − Add the following code to src/MainActivity.ktimport android.graphics.Color import android.os.Bundle import android.widget.LinearLayout import android.widget.TextView import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() {    override fun onCreate(savedInstanceState: Bundle?) {       super.onCreate(savedInstanceState)       setContentView(R.layout.activity_main)       title = "KotlinAp"       val linearLayout: LinearLayout = ...

Read More

Web Scraping in Android Application using Kotlin?

Azhar
Azhar
Updated on 05-Nov-2020 2K+ Views

This example demonstrates how to perform Web Scraping in Android Application using Kotlin.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.         Step 3 − Add the following code to src/MainActivity.ktimport android.os.AsyncTask import android.os.Bundle import android.widget.Button import android.widget.TextView import androidx.appcompat.app.AppCompatActivity import org.jsoup.Jsoup import java.io.IOException @Suppress("DEPRECATION") class MainActivity : AppCompatActivity() {    private lateinit var textView: TextView    lateinit var button: Button    override fun onCreate(savedInstanceState: Bundle?) {       super.onCreate(savedInstanceState) ...

Read More

How to create custom Alert Dialogs in an Android App using Kotlin?

Azhar
Azhar
Updated on 05-Nov-2020 2K+ Views

This example demonstrates how to create custom Alert Dialogs in an Android App using Kotlin.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.         Step 3 − Add the following code to src/MainActivity.ktimport android.app.Dialog import android.content.Context import android.os.Bundle import android.view.View import android.widget.Button import android.widget.Toast import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() {    private val context: Context = this    override fun onCreate(savedInstanceState: Bundle?) {       super.onCreate(savedInstanceState)       ...

Read More

How to use XMLPullParser to parse XML in Android using Kotlin?

Azhar
Azhar
Updated on 05-Nov-2020 1K+ Views

This example demonstrates how to use XMLPullParser to parse XML in Android using Kotlin.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.     Step 3 − Create a Layout resource file (row.xml) and add the following code −             Step 5 − Create a new asset folder, and inside the asset folder create a Android resource file (model.xml) and add the following code −   ...

Read More

How to listen for a WebView finishing loading a URL in Android using Kotlin?

Azhar
Azhar
Updated on 05-Nov-2020 1K+ Views

This example demonstrates how to listen for a WebView finishing loading a URL in Android using Kotlin.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.             Step 3 − Add the following code to src/MainActivity.ktimport android.os.Bundle import android.webkit.WebChromeClient import android.webkit.WebView import android.webkit.WebViewClient import android.widget.Button import android.widget.TextView import android.widget.Toast import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() {    lateinit var webView: WebView    lateinit var textView: TextView    private lateinit var ...

Read More
Showing 51051–51060 of 61,297 articles
Advertisements