Found 2038 Articles for R Programming

How to find the row wise mode of strings in an R data frame?

Nizamuddin Siddiqui
Updated on 03-Nov-2021 06:26:36

364 Views

To find the row wise model of strings in an R data frame, we can use apply function along with custom function for mode, if ties will be there then first value will be chosen based on alphabetical ordering.For Example, if we have a data frame called df that contains string values then we can find the row wise mode of strings by using the command given below −df$RowM

How to deal with error 'height' must be a vector or a matrix while creating barplot?

Nizamuddin Siddiqui
Updated on 03-Nov-2021 06:13:32

3K+ Views

The error 'height' must be a vector or a matrix while creating barplot occurs when we provide data frame name instead of column names or read it with as.matrix. If we want to create bar plot for columns in a data frame then the data frame needs to be read as matrix.For Example, if we have a data frame called df then we can create the barplot of columns in df by using the command given below −barplot(as.matrix(df))ExampleFollowing snippet creates a sample data frame −df

How to find bootstrap confidence interval in R?

Nizamuddin Siddiqui
Updated on 03-Nov-2021 06:03:48

5K+ Views

The bootstrap confidence interval can be found by using the boot function. The bootstrapping is a method of finding inferential statistics with the help of sample data. It is done by drawing a large number of samples with replacement from the same values. Check out the Examples given below to understand how we can create a bootstrap confidence interval.Example 1Following snippet creates a sample data frame −x1

How to find the frequency based on intervals in R data frame?

Nizamuddin Siddiqui
Updated on 03-Nov-2021 05:52:56

1K+ Views

To create intervals, we can use cut function with seq function and if we want to find the frequency based on these intervals then we just need to use table function along with cut function. We need to properly define the values for interval inside cut function. To understand how it can be done, check out the below Examples.Example 1Following snippet creates a sample data frame −x

Find the unique pair combinations of an R data frame column values.

Nizamuddin Siddiqui
Updated on 03-Nov-2021 05:48:46

1K+ Views

To find the unique pair combinations of an R data frame column values, we can use combn function along with unique function.For Example, if we have a data frame called df that contains a column say x then we can find the unique pair combinations of all column values by using the command given below −combn(unique(df$x),2,FUN=paste,collapse=' ')Example 1Following snippet creates a sample data frame −Grp

Create a random sample by ignoring missing values in an R vector.

Nizamuddin Siddiqui
Updated on 03-Nov-2021 05:41:52

268 Views

To create a random sample by ignoring missing values in an R vector, we can use sample function and the negation of is.na with vector name.For Example, if we have a vector called X that contains some NAs then we can create a random sample of size 100 of X values by using the command given below −sample(X[!is.na(X)],100,replace=TRUE)Example 1To create a random sample by ignoring the missing values in an R vector, use the command given below −x1

How to check which value is NA in an R data frame?

Nizamuddin Siddiqui
Updated on 03-Nov-2021 05:37:48

14K+ Views

To check which value in NA in an R data frame, we can use apply function along with is.na function.For Example, if we have a data frame called df that contains some NA values then we can check which value is NA by using the command mentioned below −apply(df,2, function(x) is.na(x))This will return the data frame in logical form with TRUE and FALSE. Check out the below Examples to understand how it works.Example 1Following snippet creates a sample data frame −x1

Find the frequency of non-NA values in each row of an R dataframe.

Nizamuddin Siddiqui
Updated on 03-Nov-2021 05:30:50

197 Views

To find the frequency of non-NA values in each row of an R data frame, we can use apply function along with na.omit function.For Example, if we have a data frame called df that contains some NA values then we can find the frequency of non-NA values in each row of df by using the command as follows −apply(df,1, function(x) length(na.omit(x)))Example 1Following snippet creates a sample data frame −x1

How to find the row sums if NA exists in the R data frame?

Nizamuddin Siddiqui
Updated on 03-Nov-2021 05:15:03

4K+ Views

To find the row sums if NA exists in the R data frame, we can use rowSums function and set the na.rm argument to TRUE and this argument will remove NA values before calculating the row sums.For Example, if we have a data frame called df that contains some NA values then we can find the row sums by using the below command −df$Row_Sums

How to add zero before a vector in R?

Nizamuddin Siddiqui
Updated on 03-Nov-2021 05:04:49

427 Views

To add zero before a vector in R, we can simply use rep function and provide the number times we want to have zero in the vector.For Example, if we have a vector called X that contains three values say 1, 2, 3 and we want to add three zeros before this vector then we can use the command as given below −X

Advertisements