Found 2038 Articles for R Programming

How to multiple a matrix rows in R with a vector?

Nizamuddin Siddiqui
Updated on 12-Aug-2020 12:08:19

452 Views

When we multiple a matrix with a vector in R, the multiplication is done by column but if we want to do it with rows then we can use transpose function. We can multiply the transpose of the matrix with the vector and then take the transpose of that multiplication this will result in the multiplication by rows.ExampleConsider the below matrix −> M1 M1    [,1] [,2] [,3] [,4] [,5] [1,] 1    6   11   16    21 [2,] 2    7   12   17    22 [3,] 3    8   13   18    23 [4,] 4    9   14   19    24 [5,] 5   10   15   20    25 > V1 M1*V1    [,1] [,2] [,3] [,4] [,5] [1,] 1    6   11   16   21 [2,] 4   14   24   34   44 [3,] 9   24   39   54   69 [4,] 16  36   56   76   96 [5,] 25  50   75  100  125Row-wise Multiplication −> t(t(M1)*V1)    [,1] [,2] [,3] [,4] [,5] [1,] 1   12   33   64  105 [2,] 2   14   36   68 110 [3,] 3   16   39   72 115 [4,] 4   18   42   76 120 [5,] 5   20   45   80 125Let’s have a look at one more example −> M2 M2       [,1] [,2] [,3] [,4] [,5] [1,]   72    5   36   11   76 [2,]   61   38   17   73   25 [3,]   96    9   62   79   64 [4,]   77   53   80   78   50 [5,]   81   15   21   43   23 > V2 V2 [1] 28 20 1 68 86 > t(t(M2)*V2) [,1] [,2] [,3] [,4] [,5] [1,] 2016 100 36 748 6536 [2,] 1708 760 17 4964 2150 [3,] 2688 180 62 5372 5504 [4,] 2156 1060 80 5304 4300 [5,] 2268 300 21 2924 1978

How to create a vector with names of its elements in one line code in R?

Nizamuddin Siddiqui
Updated on 12-Aug-2020 12:00:39

688 Views

Vectors are frequently created in R but most of the times we don’t give names to their elements and if we want to give their names then we can use setNames function. This function will help us to name the vector elements in a single line of code, obviously this will save our time and workspace in R.Examples > V1 V1 A B C D E F G H I J 1 2 3 4 5 6 7 8 9 10 > V2 V2 A B C D E F G H I J 1 2 3 4 ... Read More

How to plot a function with ggplot2 in R?

Nizamuddin Siddiqui
Updated on 12-Aug-2020 11:57:00

4K+ Views

Plotting a function is very easy with curve function but we can do it with ggplot2 as well. Since ggplot2 provides a better-looking plot, it is common to use it for plotting instead of other plotting functions. To plot a function, we should specify the function under stat_function in ggplot.ExampleConsider the below data frame −> x df library(ggplot2)Plotting of functions is as shown below:> ggplot(df, aes(x))+ + stat_function(fun=function(x) log(x))> ggplot(df, aes(x))+ + stat_function(fun=function(x) log(x)/x)Output> ggplot(df, aes(x))+ + stat_function(fun=function(x) log(x)/(x-3))Output> ggplot(df, aes(x))+ + stat_function(fun=function(x) (exp(x)^2)*2)OutputRead More

How to change the color of lines for a line chart using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 12-Aug-2020 11:54:25

742 Views

When we create line chart with each of the lines having different color, we might want to change the color of lines if the colors we used at the first time are not making the chart attractive. This can be done by manually setting the color of the lines in the chart with the help of scale_color_manual function.ExampleConsider the below data frame −> set.seed(2) > Group Time Frequency df df   Group Time Frequency  1 1 Time1  3  2 2 Time2  6  3 3 Time1  5  4 4 Time2  3  5 5 Time1  9  6 1 Time2  9  7 ... Read More

How to view saved Rdata file in windows?

Nizamuddin Siddiqui
Updated on 12-Aug-2020 11:50:46

1K+ Views

We save our data files created in R to use them in the future and these files have an extension .Rdata. To view these files, we can make use of load function that will read the path of the file on your system. Suppose you save the file in Documents folder as I do then you will have to provide the path of the Documents folder and that’s it.ExampleSuppose that you created a data frame df and saved it as df.Rdata file in your system −> set.seed(99) > x1 x2 x3 df df x1 x2 x3 1 2 0.7542310 3.3730539 ... Read More

How to plot a function in R?

Nizamuddin Siddiqui
Updated on 12-Aug-2020 10:05:20

404 Views

Plotting a function in R is not a difficult task. We can do it simply with curve function but if the function is very complex then it inside curve function might be difficult. It totally depends on the understand of the person who wants to plot the function, if he or she is well versed with the function then it won’t take much time, otherwise it becomes tedious.Example> curve(exp(x),from=0, to=10)Output> curve((x-1)/(x^2),from=0, to=20,ylab="y")Output> curve(((exp(x))^2)/(x),from=20, to=100,ylab="y")Output

How to filter column values for some strings from an R data frame using dplyr?

Nizamuddin Siddiqui
Updated on 12-Aug-2020 10:00:32

222 Views

Filtering data helps us to make desired groups of data than can be further used for analysis. In this way, accuracy can be achieved and computation becomes easy. Suppose, we have a homogeneous group then to partition that group based on some characteristics the filter function of dplyr package can be used.ExampleConsider the below data frame −> Subject Score df head(df, 20) Subject Score 1 Stats 88 2 Stats 20 3 Stats 49 4 Stats 31 5 Stats 83 6 Physics 29 7 Physics 43 8 Physics 73 9 Physics 28 10 Physics 74 11 Physics 93 12 Physics ... Read More

How to find the unique rows based on some columns in R?

Nizamuddin Siddiqui
Updated on 12-Aug-2020 09:55:01

366 Views

Especially when the experimental conditions are same then we expect some of the row values for some columns to be the same, it is also done on purpose while designing the experiments to check the fixed effect of variables. If we want to determine the unique rows then it can be done by using unique function in R.ExampleConsider the below data frame −> x1 x2 x3 df df x1 x2 x3  1 1 1 A  2 1 1 B  3 1 2 C  4 1 2 D  5 2 2 E  6 2 2 F  7 2 3 G  8 ... Read More

How to remove partial string after a special character in R?

Nizamuddin Siddiqui
Updated on 12-Aug-2020 09:51:38

478 Views

Sometimes we don’t require the whole string to proceed with the analysis, especially when it complicates the analysis or making no sense. In such type of situations, the part of string which we feel that is not necessary can be removed from the complete string. For example, suppose we have a string ID:00001-1 but we don’t want -1 in this string then we can remove it and this can be done with the help of gsub function.Example> x1 gsub("\-.*", "", x1) [1] "ID:00001" "ID:00100" "ID:00201" "ID:014700" "ID:12045" "ID:00012" "ID:10078" > x2 gsub("\/.*", "", x2) [1] "ID:00001" "ID:00100" "ID:00201" "ID:014700" "ID:12045" ... Read More

How to convert a string in an R data frame to NA?

Nizamuddin Siddiqui
Updated on 12-Aug-2020 09:42:56

377 Views

We often see mistakes in data collection processes and these mistakes might lead to incorrect results of the research. When the data is collected with mistakes, it makes the job of analyst difficult. One of the situations, that shows the data has mistakes is getting strings in place of numerical values. Therefore, we need to convert these strings to NA in R so that we can proceed with our intended analysis.ExampleConsider the below data frame −> x1 x2 df df  x1 x2 1 1 67 2 3 67 3 6 67 4 7 67 5 5 XYZ 6 2 XYZ ... Read More

Advertisements