ExampleApproachAsk the user to enter the size of array.Make a string array of given size.Ask the user to enter elements.At the end, print the array.Examplepackage main import "fmt" func main(){ arr := []int{10, 20, 30, 60, 40, 50} boolArr := []bool{true, false, true, false, true, false} fmt.Println("Input Array is: ", arr) fmt.Println("Input Boolean Array is: ", boolArr) visitedArray := []struct{ i int b bool }{ {10, true}, {20, false}, {30, true}, {60, false}, {40, true}, {50, false}, } fmt.Println("Boolean array using struct: ", visitedArray) }OutputInput Array is: [10 20 30 60 40 50] Input Boolean Array is: [true false true false true false] Boolean array using struct: [{10 true} {20 false} {30 true} {60 false} {40 true} {50 false}]
To create a bar plot in R, we can use barplot function but if there exist some missing values in the data then we can use ggplot2 package. For example, if we have a data frame having two vectors say x and y, x containing categorical values with NA as one of the values and y having counts/frequency for each of the categories then the bar plot will be created by using the command ggplot(df, aes(x, y))+geom_bar(stat="identity").ExampleConsider the below data frame −> x y df dfOutput x y 1 A 24 2 B 21 3 45Creating ... Read More
To check if a vector exists in a list, we can use %in%, and read the vector as list using list function. For example, if we have a list called LIST and a vector called V then we can check whether V exists in LIST using the command LIST %in% list(V).ExampleConsider the below list −List
ExampleAbs(x, y) => √(x)2+(y)2Scale(f) => (x*f, y*f)ApproachDefine a vertex as a struct.Initialize the vertex with some x and value.Define a method to calculate absolute(x, y).Define a method to calculate scale(x*f, y*f).Examplepackage main import ( "fmt" "math" ) type Vertex struct { X, Y float64 } func Abs(v Vertex) float64{ return math.Sqrt(v.X*v.X + v.Y*v.Y) } func Scale(v *Vertex, f float64) { v.X = v.X * f v.Y = v.Y * f } func main() { v := Vertex{3, 4} fmt.Println("Given vertex is: ", v) fmt.Println("Absolute value of given vertex is: ", Abs(v)) ... Read More
To split a data frame using row number, we can use split function and cumsum function. The split function will split the rows and cumsum function will select the rows. For example, if we have a data frame called df that contains twenty rows then we can split into two data frames at row 11 by using the below command −split(df,cumsum(1:nrow(df)%in%11)).ExampleConsider the below data frame −x1
If a matrix has multiple columns and values in each row are different then there will be number of maximums equal to the number of columns. Suppose, we want to extract the index of two maximums in each row in a matrix called M then we can use the below command −t(apply(M,1,order,decreasing=TRUE)[1:2,]) Example1> M1 M1Output [,1] [,2] [,3] [,4] [1,] 7 4 4 10 [2,] 7 1 4 3 [3,] 9 6 6 4 [4,] 6 5 3 3 [5,] 3 3 2 4 [6,] 5 6 2 2 [7,] 4 7 3 10 [8,] 6 0 11 6 [9,] 1 6 2 2 [10,] 4 9 4 9 [11,] 4 10 3 6 [12,] 3 9 3 11 [13,] 5 4 7 2 [14,] 6 7 6 6 [15,] 2 5 6 3 [16,] 2 5 8 2 [17,] 0 3 1 6 [18,] 6 10 6 4 [19,] 3 4 5 5 [20,] 4 5 8 4Finding the index of two maximums in each of M1 −> t(apply(M1,1,order,decreasing=TRUE)[1:2,])Output [,1] [,2] [1,] 4 1 [2,] 1 3 [3,] 1 2 [4,] 1 2 [5,] 4 1 [6,] 2 1 [7,] 4 2 [8,] 3 1 [9,] 2 3 [10,] 2 4 [11,] 2 4 [12,] 4 2 [13,] 3 1 [14,] 2 1 [15,] 3 2 [16,] 3 2 [17,] 4 2 [18,] 2 1 [19,] 3 4 [20,] 3 2Example2> M2 M2Output [,1] [,2] [,3] [,4] [1,] 65 52 42 63 [2,] 52 49 43 54 [3,] 50 35 49 57 [4,] 52 42 36 52 [5,] 48 36 45 43 [6,] 49 65 62 51 [7,] 52 46 56 51 [8,] 43 51 41 53 [9,] 53 40 51 55 [10,] 52 48 48 41 [11,] 54 44 48 42 [12,] 43 34 58 54 [13,] 41 50 51 45 [14,] 47 40 56 39 [15,] 49 48 42 38 [16,] 50 56 47 56 [17,] 55 48 39 52 [18,] 49 39 48 37 [19,] 53 49 58 50 [20,] 38 57 48 59Finding the index of two maximums in each of M2 −> t(apply(M2,1,order,decreasing=TRUE)[1:2,])Output [,1] [,2] [1,] 1 4 [2,] 4 1 [3,] 4 1 [4,] 1 4 [5,] 1 3 [6,] 2 3 [7,] 3 1 [8,] 4 2 [9,] 4 1 [10,] 1 2 [11,] 1 3 [12,] 3 4 [13,] 3 2 [14,] 3 1 [15,] 1 2 [16,] 2 4 [17,] 1 4 [18,] 1 3 [19,] 3 1 [20,] 4 2
A binary matrix contains values in form of twos such as 0/1, 1/2, Yes/No etc. If we have a matrix that has some values and we expect that there are only two values in the whole matrix then we can check whether only those two values exist in the matrix or not. For example, if we have a matrix called M then we can check whether it contains only 0/1 in the matrix by using the command all(M %in% 0:1).Example1> M1 M1Output [, 1] [, 2] [, 3] [, 4] [1, ] 0 0 0 ... Read More
To remove starting and ending zeros in an R vector, we can use min and max function to access values except 0 and then subsetting with single square brackets. For example, if we have a vector called x then we can remove starting and ending zeros by using the command −x[min(which(x!=0)):max(which(x!=0))]Examplex1
Suppose we have two frames each having 5 columns that are stored in a list in R and the data that belongs to same columns has some kind of inherent relationship or we want to check whether there exists a relationship between them then we might want to extract those columns. Therefore, we can use lapply function for this extraction. For example, if we have a list called LIST that store two data frames then column 3 of each data frame can be extracted by using the command lapply(LIST, "[", 3).ExampleConsider the below data frames and list of these data ... Read More
To find the location of a numerical value in an R data frame we use which function and if the value is string then the same function will be used but we need to pass the value appropriately. For example, if we have a data frame called df that contains a value say tutor then we can find the location of tutor by using the command which(df=="tutor", arr.ind=TRUE).Example1Consider the below data frame −> x1 x2 x3 df1 df1Output x1 x2 x3 1 2018 2020 2018 2 2020 2020 2015 3 2018 2020 2015 4 2018 2015 2020 ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Economics & Finance