R Programming Articles

Page 74 of 174

How to create a subset of an R data frame having complete cases of a particular column?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 399 Views

If we have missing values in a data frame then all the values cannot be considered complete cases and we might want to extract only values that are complete. We might want extract the complete cases for a particular column only. Therefore, we can use negation of is.na for the column of the data frame that we want to subset.Consider the below data frame −Exampleset.seed(123) x

Read More

How to fill the NA with last observation in the column of an R data frame?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 529 Views

There are multiple ways to fill missing values in data analysis and one of the ways is filling them with the previous value in the same column of the data frame. For example, if we have a column x in data frame df and this columns x contains some NA values then we can fill them with the values in the upper row. This can be done with the help of na.locf function of zoo package.Consider the below data frame −Exampleset.seed(477) x

Read More

How to find the root mean square of a vector in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 865 Views

To find the root mean square of a vector we can find the mean of the squared values then take the square root of the resulting vector. This can be done in a single and very short line of code. For example, if we have a vector x and we want to find the root mean square of this vector then it can be done as sqrt(mean(x^2)).Examplex1

Read More

How to round correlation values in the correlation matrix to zero decimal places in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 5K+ Views

To find the correlation matrix, we simply need to use cor function with the data frame object name. For example, if we have a data frame named as df then the correlation matrix can be found by using cor(df). But the result will have too many decimal places to represent the correlation. If we want to avoid the values after decimal places, we can use round function.Consider the mtcars data in base R −Exampledata(mtcars) cor(mtcars)Output      mpg           cyl        disp        hp         drat       ...

Read More

How to create a list of an unordered combination of elements in a string vector in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 288 Views

An unordered combination of elements means that the combination of the values in a way that does not make any particular arrangement. For example, if we have three values one, two, and three then they can be arranged in the following way which is unordered −"one" "two" "three" "one" "two" "one" three" "two" "three" "one" "two" "three"Examplex

Read More

How to subtract one data frame from another in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 10K+ Views

If we have two data frames with same number of columns of same data type and equal number of rows then we might want to find the difference between the corresponding values of the data frames. To do this, we simply need to use minus sign. For example, if we have data-frames df1 and df2 then the subtraction can be found as df1-df2.Consider the below data frame −Examplex1

Read More

How to display the values of two columns of an R data frame separately in a plot?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 1K+ Views

In general, the scatterplot is used to visualize the relationship between two columns of an R data frame but if we want to display the two columns separately not as a pair then we need to use matplot function. This function will create a plot for all the values in the two columns and represent them by their column number.Consider the below data frame −Exampleset.seed(222) x

Read More

How to change a data frame with comma separated values in columns to multiple columns in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 973 Views

Mostly, we need to import the data from an outside source in R environment for analysis and these data can be recorded as comma separated values that represent rows. If we want to create the columns for the comma separated values then cSplit function of splitstackshape package can be used. In the below example, we have created a data frame with comma separated values then splitting those values as single value in each column.Consider the below data frame −Exampledf=data.frame(x=apply(matrix(rpois(200, 10), 20, 10), 1, paste, collapse=", ")) dfoutputx 1 8, 12, 7, 12, 10, 8, 11, 6, 8, 7 2 9, ...

Read More

How to save an R data frame as txt file?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 3K+ Views

If we want to use a data frame created in R in the future then it is better to save that data frame as txt file because it is obvious that data creation takes time. This can be done by using write.table function. For example, if we have a data frame df then we can save it as txt file by using the code write.table(df,"df.txt",sep="\t",row.names=FALSE)Consider the below data frame −Exampleset.seed(111) x1

Read More

How to create a subset of a matrix in R using row names?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 757 Views

When we create a matrix in R, the row names and column names are not defined but we can define them separately. If we want to take a subset of rows of a matrix then row numbers can be used within single square brackets but if we want to do it with the names then we need to specify those names.ExampleM1

Read More
Showing 731–740 of 1,740 articles
« Prev 1 72 73 74 75 76 174 Next »
Advertisements