Programming Articles - Page 924 of 3363

How to create correlation matrix plot without variables labels in R?

Nizamuddin Siddiqui
Updated on 12-Nov-2021 04:06:03

544 Views

To create correlation matrix plot without variables labels in R, we can use tl.pos argument set to n.For Example, if we have a correlation matrix say M then we can create the correlation matrix plot without variables labels by using the below command −corrplot(M,tl.pos='n')ExampleFollowing snippet creates a sample data frame −x

How to get the current username and directory in Golang?

Kiran Kumar Panigrahi
Updated on 01-Nov-2021 07:58:39

7K+ Views

Sometimes there are cases where we want to know which user is executing the current program and in which directory and we can get all these details with the help of the user package that is present inside the os package of Go's standard library.In this article, we will explore three such cases, where first, we will log out the username who is executing the current process and then we will log out the name along with the id and finally, we will log out from the directory in which the current program is located.Getting the UsernameTo get the username, ... Read More

How to get the size of an array or a slice in Golang?

Kiran Kumar Panigrahi
Updated on 01-Nov-2021 07:47:24

981 Views

In case we want to get the size of an array or a slice, we can make use of the built-in len() function that Go provides us with.Example 1Let's first explore how to use the len function with an array. Consider the code shown below.package main import (    "fmt" ) func main() {    fmt.Println("Inside the main function")    arr := [3]int{1, 2, 3}    fmt.Println("The length of the array is:", len(arr)) }In the above code, we are using the len function on the array defined as arr.OutputIf we run the command go run main.go ... Read More

How to close a channel in Golang?

Kiran Kumar Panigrahi
Updated on 01-Nov-2021 07:45:20

3K+ Views

We can close a channel in Golang with the help of the close() function. Once a channel is closed, we can't send data to it, though we can still read data from it. A closed channel denotes a case where we want to show that the work has been done on this channel, and there's no need for it to be open.We open the channel the moment we declare one using the make keyword.ch := make(chan int)Example 1Let's consider a very simple example, in which we will create a buffered channel and then pass data to it and then close ... Read More

Extract columns with a string in column name of an R data frame.

Nizamuddin Siddiqui
Updated on 01-Nov-2021 07:58:40

4K+ Views

To extract columns with a particular string in column name of an R data frame, we can use grepl function for column names and then subset the data frame with single square brackets.For Example, if we have a data frame called df and we want to extract columns that has X in their names then we can use the command mentioned below −df[grepl("X",colnames(df))]Example 1Following snippet creates a sample data frame −Students_Score

How to check if a string is a valid URL in Golang?

Kiran Kumar Panigrahi
Updated on 01-Nov-2021 07:41:23

5K+ Views

There are cases where we would want to know if the URL that we got from an http request in the form of a string is even valid or not. In such cases, one can use two functions that the net/url package of Go's standard library provides.Example 1The first basic case is to check if the URL is well structured and a valid one, and for that, we can use the ParseRequestURI() function of the URL package.Consider the code shown below.package main import (    "fmt"    "net/url" ) func main() {    u, err := url.ParseRequestURI("http://golangcode.com")   ... Read More

How to split a string in Golang?

Kiran Kumar Panigrahi
Updated on 03-Mar-2022 13:33:43

4K+ Views

Splitting a string means being able to get a part of a string, and that part can either be based on a position or a character in the string.In Go, if we want to split a string on the basis of positions, we can make use of the [ ] (square brackets) and then pass the indices in them separated by a colon.SyntaxConsider the syntax shown below.sl[startingIndex : EndingIndex]It should be noted that the element at the EndingIndex in the slice won't be considered, as the range is from startingIndex to (EndingIndex−1).Example 1Now, let's consider an example where we will ... Read More

What are WaitGroups in Golang?

Kiran Kumar Panigrahi
Updated on 01-Nov-2021 07:36:26

245 Views

There may be instances in Golang where the execution of different goroutines may cause unexpected behaviour. In such cases, we want to make sure that certain goroutines work in a predefined manner and the program waits for all goroutines that are launched from the main function to wait. To do that, we use of WaitGroups.WaitGroups allow us to tackle the problem mentioned above as they block the code until any goroutines within the WaitGroup has been successfully executed.A WaitGroup has three exported methods that we make use of. These are −Add(int) – Increases the counter.Wait() – Blocks the execution until ... Read More

How to use Mutex in Golang?

Kiran Kumar Panigrahi
Updated on 01-Nov-2021 07:33:12

463 Views

In order to understand why mutex in Go play a significant role to write better and accurate concurrent programs, we must first be aware of the concept called Race Conditions. Let's first understand what Race conditions are and how we can write a concurrent program that features a race condition and how we can then introduce mutex in that program to make it accurate.Race ConditionA race condition is a condition in which multiple goroutines are trying to access and modify the same resource. It could be the case that one goroutines is trying to increase a value of a particular ... Read More

How to replace 0 with NA in an R matrix?

Nizamuddin Siddiqui
Updated on 01-Nov-2021 07:39:43

3K+ Views

To replace 0 with NA in an R matrix, we can use subsetting with single square brackets and then set zeros to NA.For Example, if we have a matrix called M that contains some zeros then we can replace 0 with NA by using the command mentioned below −M[M==0]

Advertisements