R Programming Articles

Page 164 of 174

How to write strings with new lines in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Aug-2020 423 Views

While writing the string vectors, we get them in a single line but we might want to represent strings in different lines especially in cases where each of the value of the string vector has a different meaning. This is helpful to the programmer as well as to any other reader. We can change the single line to multiple new lines using writeLines function in R.ExampleReading with single line −> String1 String1 [1] "Covid-19" "2020" "Lockdown" "Quarantine" "Life Changing"Reading the same vector with new lines −> String1 String2 writeLines(String2) Tutorialspoint SIMPLY EASY LEARNING You are browsing the best resource for ...

Read More

How to check whether a vector contains an NA value or not in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Aug-2020 3K+ Views

An NA value in R represents “Not Available” that means missing value. If a vector has even one NA value then the calculations for that vector becomes a little difficult because we will either have to remove that NA, replace it or neglect it during the calculations. To do any of these things, we will have to make some changes in our codes therefore, it is better to check whether a vector contain an NA or not before doing anything. This can be done with the help of any function in conjunction with is.na.Example> x1 x1 [1] 1 2 3 ...

Read More

How to move a column from other position to first position in an R data frame?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Aug-2020 317 Views

A column’s position in an R data frame is crucial specially when we are dealing with a large data set. As the first column appears first, it becomes necessary that we should have columns of the data frame in an order that helps us to look at the important columns easily. For this purpose, we might want to change the position of columns. To change the position of a column to first position we can use single square brackets.ExampleConsider the below data frame −> set.seed(99) > x1 x2 x3 x4 x5 x6 df df x1 x2 x3 x4 x5 x6 ...

Read More

How to select random elements from an R vector?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Aug-2020 10K+ Views

Random selection of elements from a R vector ensures the unbiased selection because while doing the random selection, each of the elements of the vector gets an equal probability of being selected by the random selection procedure specifically the simple random sampling selection procedure. To select, one or more elements randomly from an R vector, we can use sample function.Example> x1 x1  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 [26] 26 27 28 29 30 31 32 33 34 35 36 ...

Read More

How to change a column in an R data frame with some conditions?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Aug-2020 596 Views

Sometimes, the column value of a particular column has some relation with another column and we might need to change the value of that particular column based on some conditions. We need to make this change to check how the change in the values of a column can make an impact on the relationship between the two columns under consideration. In R, we can use single square brackets to make the changes in the column values.ExampleConsider the below data frame −> set.seed(1) > x1 x2 x3 df df x1 x2 x3 1 4 4 4.462839 2 4 1 3.941181 3 ...

Read More

How to increase the length of an R data frame by repeating the number of rows?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Aug-2020 701 Views

If we strongly believe that new data collection will result in the same type of data then we might want to stretch our data frame in R with more rows. Although, this is not recommended because we lose unbiasedness in the data due to this process but it is done to save time and money that will be invested in new data collection. In R, we can use rep with seq_len function to repeat the number of rows of an R data frame.ExampleConsider the below data frame −> x1 x2 df df x1 x2 1 Fruits 2 2 Vegetables 5 ...

Read More

How to find group-wise summary statistics for an R data frame?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Aug-2020 533 Views

To compare different groups, we need the summary statistics for each of the groups. It helps us to observe the differences between the groups. The summary statistics provides the minimum value, first quartile, median, third quartile, and the maximum values. Therefore, we can compare each of these values for the groups. To find the group-wise summary statistics for an R data frame, we can use tapply function.ExampleConsider the below data frame −> set.seed(99) > x1 x2 df head(df, 20) x1 x2 1 48 G1 2 33 G2 3 44 G3 4 22 G4 5 99 G5 6 62 G1 7 ...

Read More

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

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Aug-2020 567 Views

An R data frame contain columns that might represent a similar type of variables; therefore, we might want to find the sum of the values for each of the columns and make a comparison based on the sum. This can be done with the help of sum function but first we need to extract the columns to find the sum.ExampleConsider the below data frame −> set.seed(1) > x1 x2 x3 x4 x5 x6 x7 df df x1 x2 x3 x4 x5 x6 x7 1 -0.62645381 1.41897737 0.83547640 3.9016178 1.4313313 1.879633 2.494043 2 0.18364332 1.28213630 0.74663832 1.4607600 1.8648214 2.542116 4.343039 3 ...

Read More

How to convert all words of a string or categorical variable in an R data frame to uppercase?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Aug-2020 409 Views

Most of the times the format of the data we get is not we are looking for therefore, we need to change that according to our need. When the levels of categorical variables are represented by words instead of numbers then we can convert those levels to lowercase or to uppercase. Sometimes, this is done just to make the information look user friendly. Mostly, we find that the values are in lowercase, so we can convert it to the upper case with the help of sapply function.ExampleConsider the below data frame −> x1 x2 x3 df df   x1 x2 ...

Read More

How to select rows with group wise minimum or maximum values of a variable in an R data frame using dplyr?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Aug-2020 2K+ Views

If an R data frame contains a group variable that has many group levels then finding the minimum and maximum values of a discrete or continuous variable based on the group levels becomes difficult. But this can be done with slice function in dplyr package.Consider the below data frame that has one group variable and continuous as well as discrete variables −> set.seed(2) > x1 x2 x3 x4 x5 x6 x7 Group df df x1 x2 x3 x4 x5 x6 x7 Group 1 85 8 14 7 8 2.900301 749 1 2 79 7 12 4 3 3.331022 200 2 ...

Read More
Showing 1631–1640 of 1,740 articles
« Prev 1 162 163 164 165 166 174 Next »
Advertisements