Add One Column in an R Data Frame

Nizamuddin Siddiqui
Updated on 10-Feb-2021 05:20:45

174 Views

Data Analysis projects might require things that we generally think simple but they are actually helpful to achieve objectives, one such thing would be adding values or columns of an R data frame. To add one column to rest of the next columns, we can replace the next columns by adding the one column by using single square brackets.Consider the below data frame −Example Live Demox1

Export Data Frame in R to Excel

Nizamuddin Siddiqui
Updated on 10-Feb-2021 05:04:53

668 Views

To export data frame in R to excel can be done with the help of write.xlsx function of xlsx package. We would need to pass the data frame name, the file name and the sheet name in which we want to save the data frame. For example, if we have a data frame called df with file name Excel_df and on sheet1 then the data frame will be saved by using the below command −write.xlsx(df, file="Excel_df.xlsx", sheetName = "Sheet1")Consider the below data frame −Example Live Demox

Find Mean and Standard Deviation from Frequency Table in R

Nizamuddin Siddiqui
Updated on 09-Feb-2021 12:36:12

5K+ Views

To find the mean and standard deviation from frequency table, we would need to apply the formula for mean and standard deviation for frequency data. For example, if we have a data frame called df that contains a column x for units and frequency for counts then the mean and standard deviation can be calculated as −Mean = sum(df$x*df$frequency)/sum(df$frequency) SD = sqrt(sum((df$x−Mean)**2*df$frequency)/(sum(df$frequency)−1)) respectively.Example1 Live Demox

Find Unique Matrices in a List in R

Nizamuddin Siddiqui
Updated on 09-Feb-2021 12:33:23

246 Views

A list can contain many types of elements such as vectors, matrices, data frames etc. If we have matrices in a list then to find unique matrices in that list, we can simply use unique function. For example, if we have a list called LIST that contains matrices having some duplicate matrices then unique matrices can be extracted by using unique(LIST).Example1 Live Demolist(M1=matrix(1:25, ncol=5), M2=matrix(1:25, ncol=5), M3=matrix(1:25, ncol=5), M4=matrix(rpois(25, 5), ncol=5)) List1Output$M1 [, 1] [, 2] [, 3] [, 4] [, 5] [1, ] 1 6 11 16 21 [2, ] 2 7 12 17 22 [3, ] 3 8 13 ... Read More

Return Same Index for Consecutively Duplicated Values in R Vector

Nizamuddin Siddiqui
Updated on 09-Feb-2021 12:33:07

153 Views

It is obvious that duplicate values in an R vector do not have same indexes but we might want to create the same index for consecutively duplicated values, this will help to recognize the groups of duplicated values. For this purpose, we can use cumsum function along with diff function as shown in the below examples.Example1 Live Demox1

Change Y-Axis Title to Horizontal Using ggplot2 in R

Nizamuddin Siddiqui
Updated on 09-Feb-2021 12:32:50

4K+ Views

The default direction of Y-axis title using ggplot2 in R is vertical and we can change to horizontal. For this purpose, we can use theme function of ggplot2 package. We would need to use the argument of theme function as axis.title.y=element_text(angle=0)) and this will write the Y-axis title to horizontal but the position will be changed to top.Example Live DemoConsider the below data frame −x

Check for Palindrome in R

Nizamuddin Siddiqui
Updated on 09-Feb-2021 12:32:32

4K+ Views

A palindrome is a word or any value that is being read in the same way from right to left as in left to right. For example, 12321, 514212415, ABCDEDCBA, etc. To check palindrome in R, we can create a function using stri_reverse function of stringi package as shown in the below examples.Example1library(stringi) palindrome

Create Bar Chart Based on Two Groups in an R Data Frame

Nizamuddin Siddiqui
Updated on 09-Feb-2021 12:28:33

673 Views

To create a bar chart based on two groups, we can use geom_bar function of ggplot2 package with position argument that defines the position of the groups. For example, if we have a data frame called df that contains two categorical variable x1 and x2 and the one response variable y then the bar chart can be created by using the below command −ggplot(df,aes(x1,y,fill=x2))+geom_bar(position=position_dodge(),stat="identity")Example Live DemoConsider the below data frame &minusGender

Check If List Element is Greater Than a Certain Value in R

Nizamuddin Siddiqui
Updated on 09-Feb-2021 12:28:18

987 Views

If we have a list that contains numeric elements and we want to check whether the elements are greater than a certain value then as.numeric function can be used. The output of the function will be in 0/1 format where 0 represents FALSE and 1 represents TRUE. For example, if we have a list called LIST then to check whether elements in LIST are greater than 2 can be done as as.numeric(LIST>2).Example1 Live DemoList15)Output[1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0Example2 Live DemoList22)Output[1] 1 1 1 1 0 0 1 0 1 1 1 1 1 0 0 0 1 1 1 0

Rounding in R to Next 10 Instead of Nearest 10

Nizamuddin Siddiqui
Updated on 09-Feb-2021 12:22:00

255 Views

In general, most commonly used rounding is rounding to nearest 10 or nearest 100 but sometimes we actually want to remove the values after a value instead of rounding. For example, removing values after 2 decimal places, this is the type of situation where we need to round to next 10 instead of nearest 10. This can be done with the help of floor function as shown in the below examples.Example1 Live Demox1

Advertisements