Category: Interview Questions
-
How to parse a date from its string representation in R?
To parse a date from its string representation in R, we should use the lubridate package of the tidyverse collection. This package offers various functions for parsing a string and extracting the standard date from it based on the initial date pattern in that string. These functions are ymd(), ymd_hm(), ymd_hms(), dmy(), dmy_hm(), dmy_hms(), mdy(), mdy_hm(), mdy_hms(), etc., where y, m, d, h, m, and s…
-
How to create a new column in a data frame in R based on other columns?
1. Using the transform() and ifelse() functions of the base R: Output: 2. Using the with() and ifelse() functions of the base R: Output: 3. Using the apply() function of the base R: Output: 4. Using the mutate() function of the dplyr package and the ifelse() function of the base R: Output:
-
What is the difference between the subset() and sample() functions n R?
The subset() function in R is used for extracting rows and columns from a data frame or a matrix, or elements from a vector, based on certain conditions, e.g.: subset(my_vector, my_vector > 10). Instead, the sample() function in R can be applied only to vectors. It extracts a random sample of the predefined size from the elements of a vector,…
-
What is the difference between the str() and summary() functions in R?
The str() function returns the structure of an R object and the overall information about it, the exact contents of which depend on the data structure of that object. For example, for a vector, it returns the data type of its items, the range of item indices, and the item values (or several first values, if the…
-
What is the use of the next and break statements in R?
The next statement is used to skip a particular iteration and jump to the next one if a certain condition is met. The break statement is used to stop and exit the loop at a particular iteration if a certain condition is met. When used in one of the inner loops of a nested loop, this statement exits only…
-
What is vector recycling in R?
If we try to perform some operation on two R vectors with different lengths, the R interpreter detects under the hood the shorter one, recycles its items in the same order until the lengths of the two vectors match, and only then performs the necessary operation on these vectors. Before starting vector recycling, though, the…
-
What types of data plots can be created in R?
Being data visualization one of the strong sides of the R programming languages, we can create all types of data plots in R: The skill track Data Visualization with R will help you broaden your horizons in the field of R graphics. If you prefer to learn data visualization in R in a broader context, explore a…
-
How to chain several operations together in R?
We can chain several operations in R by using the pipe operator (%>%) provided by the tidyverse collection. Using this operator allows creating a pipeline of functions where the output of the first function is passed as the input into the second function and so on, until the pipeline ends. This eliminates the need for…
-
How to transpose two-dimensional data in R?
We can transpose a data frame or a matrix in R so that the columns become the rows and vice versa. For this purpose, we need to use the t() function of the base R. For example: Output:
-
How to concatenate strings in R?
We can concatenate two or more strings in R by using the paste() or cat() functions. The first approach is more popular. Both functions take in any number of strings to be concatenated and can also take in an optional parameter sep (along with some other optional parameters)—a character or a sequence of characters that will separate attached strings in the…