Programming Articles - Page 872 of 3368

How to add all columns of an R data frame by row?

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

221 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

How to convert the 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

How to 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

How to subset R data frame rows and keep the rows with NA in the 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

How to change the axis color in base R plot?

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

3K+ Views

To change the axis color in base R plot, we can use axis function after creating the plot.For example if we want to change the color of X-axis to blue then we can use the command given below −axis(1, col="blue")If we want to change the color of Y-axis to red then, we can use the command given below −axis(2, col="blue") Check out the below example to understand how it works.ExampleTo change the axis color in base R plot, use the code given below −plot(5)OutputIf you execute the above given code, it generates the following output −To change the axis color ... Read More

How to change the axis ticks color in base R plot?

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

1K+ Views

To change the axis ticks color in base R plot, we can use axis function after creating the plot. For example if we want to change the color of X-axis to red then we can use the command given below −axis(1, col.ticks="red")If we want to change the color of Y-axis to red then we can use the command given below −axis(2, col.ticks="red") Check out the below given example to understand how it works.ExampleTo change the axis ticks color in base R plot, use the code given below −plot(1:10)OutputIf you execute the above given code, it generates the following output −To ... Read More

How to remove at the rate sign @ at last position from every value in R data frame column?

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

117 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

How to create a column of raise to the power of another column in data frames stored in R list?

Nizamuddin Siddiqui
Updated on 12-Nov-2021 07:31:47

383 Views

To create a column of raise to the power of another column in data frames stored in R list, we can follow the below steps −First of all, create a list of data frames.Then, use lapply function to create a column of raise to the power of another column in data frames stored in the list.ExampleCreate the list of data framesUsing data.frame function to create data frames and list function to create the list of those data frames −df1

How to find the row median of columns having same name in R matrix?

Nizamuddin Siddiqui
Updated on 12-Nov-2021 07:16:42

189 Views

To find the row median of columns having same name in R matrix, we can follow the below steps −First of all, create a matrix with some columns having same name.Then, use tapply along with colnames and median function to find the row median of columns having same name.ExampleCreate the matrixLet’s create a matrix as shown below −M

How to select data.table object columns based on their class in R?

Nizamuddin Siddiqui
Updated on 12-Nov-2021 07:12:53

479 Views

To select data.table object columns based on their class in R, we can follow the below steps −First of all, create a data.table object.Then, use str function to check the structure of the object.After that, use select_if function from dplyr package to select the columns based on their class.Example 1Create the data.table objectLet’s create a data.table object as shown below −library(data.table) x1

Advertisements