Found 33676 Articles for Programming

How to extract the names of list elements in R?

Nizamuddin Siddiqui
Updated on 21-Nov-2020 06:07:43

3K+ Views

The names of list elements can be extracted by using the names function. For example, if we have a list defined as List that contains three elements with names element1, element2, and element3 then then these names can be extracted from the List by using the below command:names(List)Example1Live Demo> List1 List1Output$x1 [1] -0.04518909 -0.22779868 0.24339595 -0.86189295 -0.73387277 -0.75313131 [7] 0.39694608 2.30565359 0.55670193 0.21973762 0.62968128 -0.90936921 [13] 1.33946741 -0.16315751 0.31357793 0.40365980 -0.23639612 -2.48749453 [19] 0.52152768 -1.57059863 0.51728464 0.98177111 0.65475629 0.23715538 [25] -0.71796609 -0.42731839 0.32335282 -0.90013122 -0.84549927 -0.88358214 [31] -0.32066379 -0.98945433 0.42469849 -1.63095343 0.32584448 0.10947333 [37] 0.23486625 0.28166351 1.18432843 0.94828212 0.09452671 0.56618262 ... Read More

How to create a heatmap for lower triangular matrix in R?

Nizamuddin Siddiqui
Updated on 21-Nov-2020 06:05:37

912 Views

A heatmap is a diagrammatic representation of data where the values are represented with colours. Mostly, it is used to display data that has slight variation. We can draw it for a full matrix, an upper triangular matrix as well as a lower triangular matrix. This can be done with the help of image function.Example1Live Demo> M1 M1Output   [, 1] [, 2] [, 3] [, 4] [, 5] [, 6] [1, ] 6    9    4    7    5    4 [2, ] 6    6    4    3    7    5 [3, ] 2   ... Read More

What is the difference between $ and @ in R?

Nizamuddin Siddiqui
Updated on 21-Nov-2020 06:01:56

418 Views

If we have a data frame defined as df that contains column x, y, and z then extraction of these columns from df can be done by using df$x, df$y, and df$z. On the other hand, if we have an S4 object defined as Data_S4 that contains column x, y, and z then the extraction of these columns can be done by using Data_S4@x, Data_S4@y, and Data_S4@z.Example of a data frame:ExampleLive Demo> x1 x2 df dfOutput x1 x2 1 4 2 2 7 0 3 10 2 4 3 1 5 7 1 6 2 2 7 3 4 8 ... Read More

How to remove border of bars from histogram in base R?

Nizamuddin Siddiqui
Updated on 21-Nov-2020 05:58:53

848 Views

By default, a histogram drawn in base R has black color borders around bars. We might want to remove these black borders to make the histogram visually smooth. This can be done by using lty argument with the hist function. For example, if we have a vector x and we want to create a histogram of x without border of bars then we can use the argument as hist(x,lty="blank").Example> x hist(x)Output:Creating histogram of x without border of bars:Example> hist(x,lty="blank")Output:

How to create an S4 object in R?

Nizamuddin Siddiqui
Updated on 21-Nov-2020 05:57:01

1K+ Views

To create an S4 object, we can use setClass function where we will pass the object name, column names, and the type of the data that will be stored in the columns. For example, if we want to create an S4 with name data and two numerical columns called by x and y then we can use setClass("data", representation(x1="numeric", x2="numeric")).Example1> setClass("data1", representation(x1="numeric", x2="numeric")) > data1 data1OutputAn object of class "data1" Slot "x1": [1] -0.586187627 0.853689097 -0.602612795 -2.194235741 -1.318522292 [6] -0.984882420 0.273584140 0.364691611 1.025472248 1.198547297 [11] -0.709282551 -0.001441127 -0.201348012 1.296811172 1.520093861 [16] 2.071031215 0.472877022 0.616211695 0.642165615 -0.122773000 Slot "x2": [1] ... Read More

How to subset nth row from an R data frame?

Nizamuddin Siddiqui
Updated on 21-Nov-2020 05:55:47

2K+ Views

We can find subsets using many ways in R and the easiest way is to use single-square brackets. If we want to subset a row or a number of consecutive or non-consecutive rows then it can be directly done with the data frame name and the single-square brackets. For example, if we have a data frame called df and we want to subset 1st row of df then we can use df[1, ] and that’s it.ExampleConsider the below data frame:Live Demo> set.seed(214) > x y z a b c q w df1 df1Outputx y z a b c q w ... Read More

What are the different types of point available in geom_point of ggplot2 package in R?

Nizamuddin Siddiqui
Updated on 21-Nov-2020 05:53:21

258 Views

We can create a point chart using ggplot2 package but that point not necessarily to be in circular shape, we have twenty-five shape options for those points in ggplot2. While creating a point chart using ggplot2, we can use shape argument inside geom_point to see the difference among these twenty-five shapes.ExampleConsider the below data frame:Live Demo> set.seed(1957) > x y df dfOutput x y 1 0.7028704 1.6664500 2 0.9672393 1.0456639 3 1.3102736 0.2495795 4 0.3389941 0.2141513 5 0.5867095 0.4417377 6 0.4257543 0.6533757 7 0.9106756 0.3611954 8 1.0444729 1.3770588 ... Read More

How to perform chi square test for goodness of fit in R?

Nizamuddin Siddiqui
Updated on 21-Nov-2020 05:50:53

4K+ Views

The chi square test for goodness of fit is a nonparametric test to test whether the observed values that falls into two or more categories follows a particular distribution of not. We can say that it compares the observed proportions with the expected chances. In R, we can perform this test by using chisq.test function. Check out the below examples to understand how it is done.Example1Live Demo> x1 x1Output[1] 9 4 1 9 6 6 1 6 0 0 5 8 8 3 7 8 0 3 3 9 6 0 3 8 2 0 8 5 9 1 3 ... Read More

How to replace a sub-string with the reverse of that sub-string in R?

Nizamuddin Siddiqui
Updated on 21-Nov-2020 05:48:52

137 Views

The chartr function in base R helps us to replace old strings with new strings and hence it can be also used to replace a subs-string with the reverse of that substring. For example, if we have a vector say x that contains tutorialpsoint and we want to convert it to tutorialspoint then it can be done as chartr("tutorialpsoint ", " tutorialspoint ", x).Example1Live Demo> x1 x1Output[1] "IDNIA"Example> chartr("DN", "ND", x1)Output[1] "INDIA" Example2Live Demo> x2 x2Output[1] "IDNIA" "IDNIA" "IDNIA" "IDNONESIA" "IDNIA" "IDNONESIA" [7] "IDNONESIA" "IDNIA" "IDNONESIA" "IDNIA" "IDNIA" "IDNONESIA" [13] "IDNONESIA" "IDNONESIA" "IDNIA" "IDNONESIA" "IDNIA" "IDNIA" [19] "IDNONESIA" "IDNONESIA" "IDNIA" ... Read More

How to create a vector of lists in R?

Nizamuddin Siddiqui
Updated on 21-Nov-2020 05:46:58

522 Views

If we have many lists but we want to use the values in the lists as a vector then we first need to combine those lists and create a vector. This can be done by using unlist function along with the combine function c to create the vector. For example, if we have two lists defined as List1 and List2 and we want to create a vector V using these lists then it can be created as:V x1 x1Output$a [1] -0.6972237 -1.5013768 -0.2451809 -0.2365569 -1.6304919 -1.1704378 [7] 1.1617054 -0.2349498 -1.2582229 0.4112065 $b [1] 2 0 2 6 0 0 ... Read More

Advertisements