Found 2038 Articles for R Programming

Check if any value in an R vector is greater than or less than a certain value.

Nizamuddin Siddiqui
Updated on 02-Nov-2021 07:55:05

9K+ Views

To check if any value in an R vector is greater than or less than a certain value, we can use any function.For Example, if we have a vector called V and we want to check if any value in V is greater than 100 then we can use the command given below −any(V>100)Similarly, we can check if any value is less than 100 by using the command as follows −any(V

How to find the sum product of two matrix by row in R?

Nizamuddin Siddiqui
Updated on 02-Nov-2021 07:39:45

537 Views

To find the sum product of two matrix by row in R, we can use rowSums function by passing the multiplication of the matrices.For example, if we have two matrices say Matrix1 and Matrix2 then, the sum product of these two matrices by row can be found by using the following command −rowSums(Matrix1*Matrix2)Example 1Following snippet creates a matrix −M1

How to multiply each value in a column by a constant in R?

Nizamuddin Siddiqui
Updated on 02-Nov-2021 07:33:26

7K+ Views

To multiply each value in a column by a constant, we can use multiplication sign *.For example, if we have a data frame called df that contains a column say x. Now, if we want to multiply each value in x with 10 then we can use the below mentioned command −df$x

How to deal with error invalid xlim value in base R plot?

Nizamuddin Siddiqui
Updated on 02-Nov-2021 07:32:58

3K+ Views

The error invalid xlim value occurs when we incorrectly provide the xlim values, these values are not used in sequence, we just need to provide first and the last value. For Example, if we want to create a point chart of 1 to 5 with xlim having values between -5 to 15 then we can use the command as follows −plot(1:5, xlim=c(-5:15))ExampleAdd the following code to the above command −plot(1:10) OutputIf you execute the above given command, it generates the following Output −Add the following code to the above snippet −plot(1:10) plot(1:10, xlim=c(-15:15)) Error in plot.window(...) : invalid 'xlim' value ... Read More

How to create histogram for discrete column in an R data frame?

Nizamuddin Siddiqui
Updated on 02-Nov-2021 07:19:31

3K+ Views

To create histogram for discrete column in an R data frame, we can use geom_bar function of ggplot2 package and set the width to 1 also passing same column for x and y in aes.For example, if we have a data frame called df that contains a discrete column say x then the histogram for data in x can be created by using the below given command −ggplot(df,aes(x,x))+geom_bar(stat="identity",width=1)ExampleFollowing snippet creates a sample data frame −x

How to create a column with ratio of two columns based on a condition in R?

Nizamuddin Siddiqui
Updated on 02-Nov-2021 07:17:25

573 Views

To create a new column with ratio of two columns based on a condition in an R data frame, we can use division sign with ifelse function.For example, if we have a data frame called df that contains two columns say X and Y and we want to create a new column with ratio of X and Y based on a condition that X is greater than 5 then we can use the below given command −df$Ratio_X_Y5,X/Y,NA))Example 1Following snippet creates a sample data frame −x1

Extract string vector elements up to a fixed number of characters in R.

Nizamuddin Siddiqui
Updated on 02-Nov-2021 07:26:05

450 Views

To extract string vector elements up to a fixed number of characters in R, we can use substring function of base R.For Example, if we have a vector of strings say X that contains 100 string values and we want to find the first five character of each value then we can use the command as given below −substring(X,1,5)Example 1Following snippet creates a sample data frame −x1

How to display X-axis tick marks as minimum and maximum only without their values using plotly in R?

Nizamuddin Siddiqui
Updated on 02-Nov-2021 07:08:52

397 Views

To display X-axis tick marks as minimum and maximum only without their values using plotly, we can use layout function of plot_ly package where we can pass the values for minimum and maximum using xaxis argument and the text using ticktext argument as shown in the below example.ExampleFollowing snippet creates a sample data frame −x

How to find the intersection of elements in a string vector in R?

Nizamuddin Siddiqui
Updated on 02-Nov-2021 07:05:50

555 Views

If we have a string vector that contains more than one element then there might exists some common values in all the elements. If we want to find those values then intersect function can be used along strsplit function and Reduce function.Check out the below Examples to understand how it can be done.Example 1>x1=c("Data science is an interdisciplinary field that uses scientific methods, processes, algorithms and systems to extract knowledge and insights from structured and unstructured data, and apply knowledge and actionable insights from data across a broad range of application domains.", "Data science is the domain of study that ... Read More

How to find the sum of values based on two groups if missing values are present in R?

Nizamuddin Siddiqui
Updated on 02-Nov-2021 07:03:53

254 Views

To find the sum of values based on two groups if missing values are present, we can use group_by and summarise function of dplyr package.For example, if we have a data frame called df that contains a numerical column say Num and two grouping columns say Grp1 and Grp2 then, the sum of values in Num based on Grp1 and Grp2 if missing values are present in df can be found by using the below mentioned command −df%>%group_by(Grp1, Grp2)%>%summarise(Sum=sum(Num, na.rm=TRUE))Example 1Following snippet creates a sample data frame −grp1Read More

Advertisements