Programming Articles

Page 1130 of 2547

How to find the frequency table for factor columns in an R data frame?

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

If we have factor columns in an R data frame then we want to find the frequency of each factor level for all the factor columns. This can be done with the help of sapply function with table function. For example, if we have a data frame called df that contains some factor columns then the frequency table for factor columns can be created by using the command sapply(df, table).Example1Consider the below data frame −> x1 x2 df1 df1Output   x1 x2 1  D  a 2  D  b 3  D  c 4  D  b 5  D  c 6  C  a 7 ...

Read More

How to set the text position using geom_text in R?

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

To set the text position using geom_text, we can use the value for the X-axis and Y-axis with appropriate positions. We need to make sure that the values we set for both the axes do not lie within the data otherwise the text will be printed on the plot we want to draw and it will become less attractiveExampleConsider the below data frame −x

Read More

How to create a date vector with randomization in R?

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

To create a date vector with randomization, we can use sample function but the dates need to be read with as.Date function. For example, if we have 2 dates say 2021-01-01 and 2021-02-02 then a vector with randomization of these three dates can be created by using the command −sample(c(as.Date("2021-01-01"),as.Date("2021-02-02")),100,replace=TRUE)Example1Date1

Read More

How to extract the first digit from a character column in an R data frame?

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

If we have a character column in the data frame that contains string as well as numeric values and the first digit of the numeric values has some meaning that can help in data analysis then we can extract those first digits. For this purpose, we can use stri_extract_first function from stringi package.Example1Consider the below data frame −> x1 y1 df1 df1Output x1 y1 1 1 HT14L 2 2 HT14L 3 3 HT23L 4 4 HT14L 5 5 HT32L 6 6 HT32L 7 7 ...

Read More

How to change the position of missing values to the end of data frame in R?

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

Most of the times we need to deal with missing values in data science projects and these missing values can be occurred at any position. We might want to change the position of these missing values and send them to the end of the columns in the data frame. This can be done with the help of lapply function as shown in the below examples.Example1Consider the below data frame −> x1 x2 x3 df1 df1Output   x1 x2 x3 1  0  0  2 2  1  1  NA 3  1  NA 0 4  0  NA 2 5  1  NA 2 6  NA ...

Read More

How to find the index of the nearest smallest number in an R data frame column?

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

To find the index of the nearest smallest number in an R data frame column, we can use which function along with subsetting for the value for which we want to find the index of the nearest smallest number. To understand how it can be done check out the below examples.Example1Consider the below data frame −ID

Read More

How to convert an old data frame to new data frame in R?

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

To convert an old data frame to a new data frame, we can simply set the new name. For example, if we have a data frame called df and want to convert it to a new one let’s say df_new then it can be done as df_new x1 x2 df1 df1Output x1 x2 1 8 6 2 4 9 3 3 2 4 3 5 5 7 4 6 4 8 7 8 6 8 12 12 9 8 6 10 ...

Read More

How to create a data frame column with letters of both size in R?

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

To create a data frame column with letters of both sizes, we can simply use the letters function and LETTERS function, the first one corresponds to lowercase letters and the latter corresponds to uppercase letters with single square brackets as shown in the below examples.Example1df1

Read More

Golang program to define a binary tree

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 11-Mar-2026 702 Views

Example − In the given tree, the root node is 1, the root of its left sub tree is 2, and the root of its right sub tree is 3, ... so on.Preorder Tree Traversal Output: 1, 2, 4, 5, 3, 6, 7.Approach to solve this problemStep 1 − First, we’ll define the node structure.Step 2 − In the main method, we would create the above tree.Step 3 − Finally, we will perform the Preorder Tree Traversal.Examplepackage main import "fmt" type Node struct {    data int    left *Node    right *Node } func (root *Node)PreOrderTraversal(){    if root ...

Read More

How to find the mean squared error for linear model in R?

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

To find the mean squared error for linear model, we can use predicted values of the model and find the error from dependent variable then take its square and the mean of the whole output. For example, if we have a linear model called M for a data frame df then we can find the mean squared error using the command mean((df$y-predict(M))^2).Example1Consider the below data frame −x1

Read More
Showing 11291–11300 of 25,466 articles
Advertisements