Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Server Side Programming Articles
Page 1130 of 2109
How to extract data frame columns stored in a list in R?
Suppose we have two frames each having 5 columns that are stored in a list in R and the data that belongs to same columns has some kind of inherent relationship or we want to check whether there exists a relationship between them then we might want to extract those columns. Therefore, we can use lapply function for this extraction. For example, if we have a list called LIST that store two data frames then column 3 of each data frame can be extracted by using the command lapply(LIST, "[", 3).ExampleConsider the below data frames and list of these data ...
Read MoreHow to extract the last element in an R matrix?
To extract the last element in an R matrix, we can use the length function along with single square brackets that are used for subsetting. For example, if we have a matrix called M as shown below −M1 2 3 4 5 6 7 8 9then we can extract last value of M by using the command M[length(M)].ExampleConsider the below matrix −M1
Read MoreHow to find the sum of every n values in R data frame columns?
To find the sum of every n values in R data frame columns, we can use rowsum function along with rep function that will repeat the sum for rows. For example, if we have a data frame called df that contains 4 columns each containing twenty values then we can find the column sums for every 5 rows by using the command rowsum(df,rep(1:5,each=4)).ExampleConsider the below data frame −x1
Read MoreHow to get the combinations for a range of values with repetition in R?
The combination of values with repetition is the combination where the values can be repeated when creating the combination. For example, if we have three values say 1 and 2 then the combination of these values with repetition will be as follows −1 1 2 1 1 2 2 2For this purpose, we can use expand.grid function as shown in the below examples.Example 1expand.grid(rep(list(1:2),2))Output Var1 Var2 1 1 1 2 2 1 3 1 2 4 2 2Example2expand.grid(rep(list(1:2),3))Output Var1 Var2 Var3 1 1 1 1 2 2 1 1 3 1 2 1 4 2 2 1 5 1 1 2 6 2 1 2 7 1 2 2 8 2 2 2Example3expand.grid(rep(list(1:2),4))Output Var1 Var2 Var3 Var4 1 1 1 1 1 2 2 1 1 1 3 1 2 1 1 4 2 2 1 1 5 1 1 2 1 6 2 1 2 1 7 1 2 2 1 8 2 2 2 1 9 1 1 1 2 10 2 1 1 2 11 1 2 1 2 12 2 2 1 2 13 1 1 2 2 14 2 1 2 2 15 1 2 2 2 16 2 2 2 2Example4expand.grid(rep(list(1:2),5))Output Var1 Var2 Var3 Var4 Var5 1 1 1 1 1 1 2 2 1 1 1 1 3 1 2 1 1 1 4 2 2 1 1 1 5 1 1 2 1 1 6 2 1 2 1 1 7 1 2 2 1 1 8 2 2 2 1 1 9 1 1 1 2 1 10 2 1 1 2 1 11 1 2 1 2 1 12 2 2 1 2 1 13 1 1 2 2 1 14 2 1 2 2 1 15 1 2 2 2 1 16 2 2 2 2 1 17 1 1 1 1 2 18 2 1 1 1 2 19 1 2 1 1 2 20 2 2 1 1 2 21 1 1 2 1 2 22 2 1 2 1 2 23 1 2 2 1 2 24 2 2 2 1 2 25 1 1 1 2 2 26 2 1 1 2 2 27 1 2 1 2 2 28 2 2 1 2 2 29 1 1 2 2 2 30 2 1 2 2 2 31 1 2 2 2 2 32 2 2 2 2 2
Read MoreHow to display p-value with coefficients in stargazer output for linear regression model in R?
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 −x1
Read MoreHow to create a frequency column for categorical variable in an R data frame?
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 −Country
Read MoreHow to perform paired t test for multiple columns in R?
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 MoreWhat to do if numeric values are being read as character in R?
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 −x1
Read MoreHow to create a table for the number of unique values in list of vectors in R?
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 −x1
Read MoreHow to find the position of odd numbers in an R vector?
To find the position of odd numbers in an R vector, we can find the position of values that are divisible by 2 with the help of which function. For example, if we have a vector called x then we can find the position of odd numbers using the command which(x%%2==1). Check out the below examples to understand how it works.Examplex1
Read More