Programming Articles - Page 1359 of 3366

How to extract vector using different index for columns in an R matrix?

Nizamuddin Siddiqui
Updated on 06-Mar-2021 05:03:52

283 Views

Suppose we have a matrix and a vector containing indices of equal size as the matrix then we can extract the vector from matrix using the index vector. For this purpose, we can use cbind function as shown in the below examples.Example1Live Demo> M1 M1Output      [,1] [,2]  [1,]    4    0  [2,]    1    1  [3,]    1    2  [4,]    2    0  [5,]    3    2  [6,]    2    2  [7,]    1    6  [8,]    1    2  [9,]    3    1 [10,]    1    2 [11,]    2    3 [12,]    2    0 [13,]    3    0 [14,]    0    1 [15,]    2    4 [16,]    1    1 [17,]    3    1 [18,]    0    2 [19,]    2    1 [20,]    2    0Example> Index_M1 Index_M1Output[1] 2 1 2 1 2 2 1 1 2 1 1 2 1 1 1 1 2 2 1 1Example> M1[cbind(seq_along(Index_M1),Index_M1)]Output[1] 0 1 2 2 2 2 1 1 1 1 2 0 3 0 2 1 1 2 2 2Example2Live Demo> M2 M2Output      [,1] [,2] [,3] [,4]  [1,]   10    9    9   11  [2,]   13    6   16    8  [3,]   11   11    8   10  [4,]   15   11    9    9  [5,]   10    8    9    9  [6,]    7   14    9   15  [7,]    8    6    8    7  [8,]    4    8    9   12  [9,]    7   12   11   10 [10,]    8    8    9   13 [11,]    9   13   11    6 [12,]   12    5   11    8 [13,]    8    6   15    8 [14,]    6   17   12    7 [15,]    8   10    9    8 [16,]   13    7   11   13 [17,]    5   10    7    7 [18,]   10   11    8    8 [19,]    5    9    9   13 [20,]    5   10    7    6Example> Index_M2 Index_M2Output[1] 3 4 3 3 3 1 3 4 4 3 1 4 3 4 4 1 2 1 1 2Example> M2[cbind(seq_along(Index_M2),Index_M2)]Output[1] 9 8 8 9 9 7 8 12 10 9 9 8 15 7 8 13 10 10 5 10

How to aggregate matrix columns by row names in R?

Nizamuddin Siddiqui
Updated on 06-Mar-2021 05:02:39

1K+ Views

To aggregate matrix columns by row names, we can use colSums with sapply and transpose the output. For example, if we have a matrix called M then the aggregate matrix columns by row names can be done using t(sapply(by(M, rownames(M), colSums), identity)).Example1Consider the below matrix −Live Demo> M1 rownames(M1) M1Output  [, 1] [, 2] B    4    6 D    2    1 B    1    5 C    0    0 A    2    3 B    1    0 B    5    3 D    1    3 C    0    1 C ... Read More

How to find the location of a string in an R data frame?

Nizamuddin Siddiqui
Updated on 06-Mar-2021 04:58:51

2K+ Views

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 −Live Demo> x1 x2 x3 df1 df1Output     x1   x2   x3 1  2018 2020 2018 2  2020 2020 2015 3  2018 2020 2015 4  2018 2015 ... Read More

How to find the correlation for data frame having numeric and non-numeric columns in R?

Nizamuddin Siddiqui
Updated on 06-Mar-2021 04:58:20

2K+ Views

To find the correlation for data frame having numeric and non-numeric columns, we can use cor function with sapply and use complete.obs for pearson method. For example, if we have a data frame called then we can use the below command to find the correlation coefficient −cor(df[, sapply(df, is.numeric)], use="complete.obs", method="pearson")Example1Consider the below data frame −Live Demo> x1 x2 x3 df1 df1Output   x1 x2 x3 1   C 11  2 2   A  3  1 3   C  4  0 4   D 10  2 5   A  1  0 6   A  4  1 7   D  4  0 ... Read More

How to check if a matrix in R is in binary form?

Nizamuddin Siddiqui
Updated on 06-Mar-2021 04:57:24

288 Views

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).Example1Live Demo> M1 M1Output      [, 1] [, 2] [, 3] [, 4]  [1, ]    0    0   ... Read More

How to find the index of n number of maximums in each row of a matrix in R?

Nizamuddin Siddiqui
Updated on 06-Mar-2021 04:56:32

156 Views

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, ]) Example1Live Demo> M1 M1Output      [, 1] [, 2] [, 3] [, 4]  [1, ]    7    4    4   10  [2, ]    7    1    4    3  [3, ]    9    6    6    4  [4, ... Read More

How to remove a character in an R data frame column?

Nizamuddin Siddiqui
Updated on 01-Nov-2023 14:50:11

50K+ Views

To remove a character in an R data frame column, we can use gsub() function which will replace the character with blank. For example, if we have a data frame called df that contains a character column say x which has a character ID in each value then it can be removed by using the command gsub("ID", "", as.character(df$x)).Example1Consider the below data frame −Live Demo> x1 x2 df1 df1Output        x1  x2 1    Male1  8 2  Female1  4 3    Male1  9 4    Male1  2 5    Male1  7 6  Female1  5 7    Male1  3 ... Read More

How to create a bar plot with bars for missing values in R?

Nizamuddin Siddiqui
Updated on 06-Mar-2021 04:55:24

1K+ Views

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 −Live Demo> x y df dfOutput     x  y 1    A 24 2    B 21 3 ... Read More

Write a C program to maintain cricketer’s information in tabular form using structures

Mandalika
Updated on 05-Mar-2021 12:14:40

7K+ Views

ProblemHow to store the cricketer’s data in tabular form in sorted order based on average runs using structures in C Programming language.SolutionLet’s try to enter the cricketer information such as name, age, no of matches and average runs he scored. It will be entered in the console at runtime using the structure concept.And try to display the information in tabular form in sorted order based on average runs scored by each person so that it is easy to identify each person's details clearly.The logic we used to sort the cricketers in ascending order based on average runs they scored is ... Read More

Write a C program to calculate the average word length of a sentence using while loop

Mandalika
Updated on 05-Mar-2021 12:11:51

955 Views

ProblemEnter a sentence at run time and write a code for calculating the average length of words that are present in a sentenceSolutionAlgorithmSTART Step 1: declare character, int and double variables Step 2: Enter any statement Step 3: while loop        Check condition stmt[i]=getchar()) != ''        True then enter into loop        Increment I and call the function at step 5 Step 4: Print the average length return by function        From step 5 Step 5: called function calculatewordlength          i. declare and initialize         ... Read More

Advertisements