Found 2038 Articles for R Programming

How to generate random numbers with sequence and store in a data frame column in R?

Nizamuddin Siddiqui
Updated on 16-Mar-2021 12:49:01

742 Views

To generate random numbers with sequence and storing them in a data frame column, we can use sample function with seq. For example, if we want to create a data frame column having random sequence of values of size 50 between 1 to 100 by considering every tenth value then we can use the commanddf

How to create a random vector for a range of values in R?

Nizamuddin Siddiqui
Updated on 16-Mar-2021 12:44:30

2K+ Views

To create a random vector for a range of values, we can use sample function. We just need to pass the range and the sample size inside the sample function. For example, if we want to create a random sample of size 20 for a range of values between 1 to 100 then we can use the command sample(1:100,20) and if the sample size is larger than 100 then we can add replace=TRUE as shown in the below examples.Example Live Demox1

How to create a table for the number of unique values in list of vectors in R?

Nizamuddin Siddiqui
Updated on 16-Mar-2021 12:42:30

148 Views

To create a table for the number of unique values in list of vectors, we can use mtabulate function of qdapTools package. For example, if we have a list of vectors say LIST that contains some vectors then the table for the number of unique values in the vectors of LIST can be found by using mtabulate(LIST).ExampleConsider the below list − Live Demox1

How to find the sum of squared values of an R data frame column?

Nizamuddin Siddiqui
Updated on 16-Mar-2021 12:38:50

7K+ Views

To find the sum of squared values of an R data frame column, we can simply square the column with ^ sign and take the sum using sum function. For example, if we have a data frame called df that contains a column say V then the sum of squared values of V can be found by using the command sum(df$V^2).ExampleConsider the below data frame − Live DemoID

What to do if numeric values are being read as character in R?

Nizamuddin Siddiqui
Updated on 16-Mar-2021 12:36:14

3K+ Views

If the numeric values are being read as character then we need to convert them into numeric values by using the function as.numeric. For example, if we have a data frame called df that contains a column say x which has numerical values stored in character format then we can convert them into numeric values using the command as.numeric(df$x).ExampleConsider the below data frame − Live Demox1

How to find the number of unique values for each column in data.table object in R?

Nizamuddin Siddiqui
Updated on 16-Mar-2021 12:33:20

581 Views

To find the number of unique values for each column in data.table object, we can use uniqueN function along with lapply. For example, if we have a data.table object called DT that contains five columns each containing some duplicate values then the number of unique values in each of these columns can be found by using DT[,lapply(.SD,uniqueN)].ExampleConsider the below data.table object −x1

How to perform paired t test for multiple columns in R?

Nizamuddin Siddiqui
Updated on 16-Mar-2021 12:29:30

2K+ Views

When we have a factor column in an R data frame that has two levels and multiple numerical columns then we can apply paired-test on this data frame but the data must be collected for same subjects, otherwise it will not be a paired data. The t.test application on the data discussed here can be done by using the command lapply(df[-1], function(x) t.test(x~df$group)), where group is the factor column and lies at the first position in the data frame, x contains all the numerical columns in the data frame, and all these columns are stored in data frame called df.ExampleConsider ... Read More

How to create a frequency column for categorical variable in an R data frame?

Nizamuddin Siddiqui
Updated on 16-Mar-2021 12:25:10

5K+ Views

To create a frequency column for categorical variable in an R data frame, we can use the transform function by defining the length of categorical variable using ave function. The output will have the duplicated frequencies as one value in the categorical column is likely to be repeated. Check out the below examples to understand how it can be done.ExampleConsider the below data frame − Live DemoCountry

How to display p-value with coefficients in stargazer output for linear regression model in R?

Nizamuddin Siddiqui
Updated on 16-Mar-2021 12:13:21

2K+ Views

To display p-value in stargazer output for linear regression model, we can use the report argument. For example, if we have a model called RegressionModel then to display the p-value with coefficients can be done by using the below command −stargazer(RegressionModel,type="text",report=("vc*p"))ExampleConsider the below data frame − Live Demox1

How to create a blank column with randomization in an R data frame?

Nizamuddin Siddiqui
Updated on 16-Mar-2021 12:01:57

106 Views

To create a blank column with randomization in an R data frame, we can use sample function and pass the blanks with single space. For example, if we want to create a vector say x that will be added in the data frame can be created by using the command −x

Advertisements