R Programming Articles

Page 121 of 174

How to standardize selected columns in data.table object in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 08-Nov-2021 333 Views

To standardize selected columns in data.table object in R, we can follow the below steps −First of all, create a data.table object.Then, use scale function and cbind function with subsetting to standardize selected columns.ExampleCreate the data.table objectLet’s create a data.table object as shown below −library(data.table) var1

Read More

How to save a matrix as CSV file using R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 08-Nov-2021 11K+ Views

To save a matrix as CSV file using R, we can use write.matrix function of MASS package. For Example, if we have a matrix called M and we want to save it as CSV file then we can use the below mentioned command −write.matrix(M,file="Mat.csv")ExampleFollowing snippet creates a sample matrix −M

Read More

How to subset an R data frame by specifying columns that contains NA?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 08-Nov-2021 437 Views

To subset an R data frame by specifying columns that contains NA, we can follow the below steps −First of all, create a data frame with some columns containing NAs.Then, use is.na along with subset function to subset the data frame by specifying columns that contains NA.ExampleCreate the data frameLet’s create a data frame as shown below −x

Read More

Find the common elements between two columns of an R dataframe.

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 08-Nov-2021 6K+ Views

To find the common elements between two columns of an R data frame, we can use intersect function.For Example, if we have a data frame called df that contains two columns say X and Y then we can find the common elements between X and Y by using the below command −intersect(df$X,df$Y)Example 1Following snippet creates a sample data frame −x1

Read More

Find the number of non-missing values in each group of an R data frame.

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 08-Nov-2021 420 Views

To find the number of non-missing values in each group of an R data frame, we can convert the data frame to data.table object and then use the sum function with negation of is.na.For Example, if we have a data frame called df that contains a grouping column say Group and a numerical column with few NAs say Num then we can find the number of non-missing values in each Group by using the below given command −setDT(df)[,sum(!is.na(df)),by=.(Group)]Example 1Following snippet creates a sample data frame −Grp

Read More

Create stacked bar chart with percentages on Y-axis using ggplot2 in R.

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 08-Nov-2021 1K+ Views

To create stacked bar chart with percentages on Y-axis using ggplot2 in R, we can use fill argument inside geom_bar and put the second categorical variable with position set to fill.For Example, if we have a data frame called with two categorical columns say C1 and C2 then we can create stacked bar chart with percentages on Y-axis using the below mentioned command −ggplot(df,aes(C1))+geom_bar(aes(fill=C2),position="fill")ExampleFollowing snippet creates a sample data frame −f1

Read More

Subset groups that occur greater than equal to n times in R dataframe.

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 08-Nov-2021 507 Views

To subset groups that occur less than n times in R data frame, we can use filter function of dplyr package.For Example, if we have a data frame called df that contains a grouping column say Group then we can subset groups that occur less than 4 times by using the below mentioned command −df%%group_by(Group)%%filter(n()=4)Example 1Following snippet creates a sample data frame −Grp

Read More

How to find the sum of rows of a column based on multiple columns in R data frame?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 08-Nov-2021 1K+ Views

To find the sum of rows of a column based on multiple columns in R data frame, we can follow the below steps −First of all, create a data frame.Then, use aggregate function to find the sum of rows of a column based on multiple columns.ExampleCreate the data frameLet’s create a data frame as shown below −Grp1

Read More

How to display infinity symbol in base R plot?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 08-Nov-2021 1K+ Views

To display infinity symbol in base R plot, we can use text function and expression function. Inside expression function we can put infinity word for the display of infinity. For Example, if we want to display infinity at position X=2 and Y=4 then we can use the below mentioned command −text(2, 4, expression(infinity))Example 1To display infinity symbol in base R plot, use the snippet given below −plot(1:10, type="n") text(5, 5, expression(infinity))OutputIf you execute the above given snippet, it generates the following Output −Example 2To display infinity symbol in base R plot, use the snippet given below −plot(1:10, type="n") text(5, 5, ...

Read More

R programming to subtract all values in a vector from all values in another vector.

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 08-Nov-2021 4K+ Views

To subtract all values in a vector from all values in another vector in R, we can use sapply function with subtraction sign.For Example, if we have two vectors say X and Y and we want to subtract all values in Y from all values in X then we can use the command given below −sapply(X,"-",Y)Example 1Following snippet creates a sample data frame −x1

Read More
Showing 1201–1210 of 1,740 articles
« Prev 1 119 120 121 122 123 174 Next »
Advertisements