Find N Number of Quartiles for Every Row in R

Nizamuddin Siddiqui
Updated on 12-Nov-2021 08:00:26

827 Views

To find the n number of quartiles for every row in an R data frame, we can use apply function along with quantile function.For example, if we have a data frame called df that contains hundred rows and we want to find two quartiles say first and third for each row then we can use the below mentioned command −apply(df,1,quantile,c(0.25,0.75))Example 1Following snippet creates a sample data frame −x1

Deal with NA Output of Apply in R

Nizamuddin Siddiqui
Updated on 12-Nov-2021 07:56:43

685 Views

When we use apply function on numerical as well as character column then the output of the function returns NA for all hence to deal with this problem, we can use lapply function. The lapply function will take each column into account independently, therefore, the arithmetic operations will be performed individually.Check out the below given examples to understand how it works.Example 1Following snippet creates a sample data frame −x1

Subset R Data Frame Based on Small Letters

Nizamuddin Siddiqui
Updated on 12-Nov-2021 07:54:27

276 Views

If we have a data frame that contains some lower case and some upper-case string values then we might want to subset the data frame based on lower case or upper-case letters.For this purpose, we can make use of apply and sapply function as shown in the below examples.Example 1Following snippet creates a sample data frame −x1

Check Equality of Three Columns by Row in R

Nizamuddin Siddiqui
Updated on 12-Nov-2021 07:51:59

2K+ Views

To check for equality of three columns by row, we can use logical comparison of equality with double equal sign (==) and & operator.For example, if we have a data frame called df that contains three columns say C1, C2, and C3 and we want to check for equality of these three columns then we can use below given command −df$All_equal

Create a Lagged Column in an R Data Frame

Nizamuddin Siddiqui
Updated on 12-Nov-2021 07:48:02

3K+ Views

To create a lagged column in an R data frame, we can use transform function.For example, if we have a data frame called that contains a column say C and we want to create a lagged column in df based on C then we can use the command given below −transform(df,Lag_C=c(C[-1],NA))Example 1Following snippet creates a sample data frame −x

Add All Columns of an R Data Frame by Row

Nizamuddin Siddiqui
Updated on 12-Nov-2021 07:45:17

254 Views

To add all columns by row, we can use rowSums function.For example, if we have a data frame called df that contains five columns say x, y, z, a, and b and we want to add all these columns by row then we can use the below mentioned command −df$Total_sum

Convert Data Type of All Columns from Integer to Factor in R

Nizamuddin Siddiqui
Updated on 12-Nov-2021 07:39:37

3K+ Views

To convert the data type of all columns from integer to factor, we can use lapply function with factor function.For example, if we have a data frame called df which has all integer columns then we can use the below given command to convert all columns data type to factor −df

Remove '@' Sign from Last Position in R Data Frame Column

Nizamuddin Siddiqui
Updated on 12-Nov-2021 07:37:28

133 Views

To remove at the rate sign @ at last position from every value in R data frame column, we can follow the below steps −First of all, create a data frame with a column having at the rate sign @ at last position in every value.Then, use gsub function to remove the at the rate sign @ at last position from every value in the column.ExampleCreate the data frameLet’s create a data frame as shown below −Names

Take a Random Sample from a Matrix in R

Nizamuddin Siddiqui
Updated on 12-Nov-2021 07:37:14

3K+ Views

To take a random sample from a matrix in R, we can simply use sample function and if the sample size is larger than the number of elements in the matrix replace=TRUE argument will be used.For example, if we have a matrix called M that contains 100 elements and we want to sample 200 elements from M then we can use the below given command −sample(M,200,replace=TRUE)Example 1Following snippet creates a matrix −M1

Subset R Data Frame Rows Keeping NA in Output

Nizamuddin Siddiqui
Updated on 12-Nov-2021 07:34:45

3K+ Views

To subset R data frame rows and keep the rows with NA in the output, we can use subset function along with OR condition with | sign for na values.For example, if we have a data frame called df that contains a column say C which has some NA values then we can subset df for values greater than 5 and include NA in the output by using the below given command −subset(df,C>5|is.na(C))Example 1Following snippet creates a sample data frame −x1

Advertisements