R Language Quiz 15: Important MCQs

The post is about R Language Quiz with Answers. The quiz covers, MCQs about Data Structure, Data Analysis in R, and Some Basics of R Programming Languages. Let us start with the R Programming MCQs Quiz.

Multiple Choice Questions about R Language with Answers

1. What is NaN called?

 
 
 
 

2. How one can create an integer say 5?

 
 
 
 

3. Dataframes can be converted into a matrix by calling the following function data ———-

 
 
 
 

4. How many atomic vector types does R have

 
 
 
 

5. what is the class of object $y$

y <- c(2, "t")

 
 
 
 

6. R language has a superficial similarity with ———.

 
 
 
 

7. What is the output of the following

d <- diag(5, nrow = 2, ncol = 2); d

 
 
 
 

8. The dimension attribute is itself an integer vector having length ——-.

 
 
 
 

9. what is the output of the following

y <- c(TRUE, 2)

 
 
 
 

10. Which of the following is not an R object?

 
 
 
 

11. Which of them is not a basic datatype in R?

 
 
 
 

12. A ——– is a variable that holds one value at a time

 
 
 
 

13. Which of the following can be used to display the names of (most of) the objects that are currently stored within R?

 
 
 
 

14. What function is used to test objects if they are NaN?

 
 
 
 

15. R files have an extension ———-.

 
 
 
 

16. What is the meaning of “<-” in R

 
 
 
 

17. Which of the following is an alternative to ‘?’ symbol ———.

 
 
 
 

18. What is the function to give names to columns for a matrix?

 
 
 
 

19. Matrices can be created by row-binding using the function

 
 
 
 

20. What is the class of the object $y$

y <- c(FALSE, 2)

 
 
 
 

R Language Quiz with Answers

  • What is NaN called?
  • what is the output of the following y <- c(TRUE, 2)
  • what is the class of object $y$ y <- c(2, “t”)
  • What is the class of the object $y$ y <- c(FALSE, 2)
  • Which of them is not a basic datatype in R?
  • How one can create an integer say 5?
  • The dimension attribute is itself an integer vector having length ——-.
  • Matrices can be created by row-binding using the function
  • What function is used to test objects if they are NaN?
  • What is the function to give names to columns for a matrix?
  • How many atomic vector types does R have
  • What is the output of the following d <- diag(5, nrow = 2, ncol = 2); d
  • Which of the following can be used to display the names of (most of) the objects that are currently stored within R?
  • A ——– is a variable that holds one value at a time
  • What is the meaning of “<-” in R
  • Which of the following is not an R object?
  • Dataframes can be converted into a matrix by calling the following function data ———-
  • R files have an extension ———-.
  • R language has a superficial similarity with ———.
  • Which of the following is an alternative to ‘?’ symbol ———.
R Language Quiz with Answers

https://itfeature.com

https://gmstat.com

Read Data from CSV File

Introduction to Read Data From CSV File

In R Language one can easily read data from CSV file format. One can use the read.csv() function. There are different ways to read the CSV file in R and the read.csv() function has many useful arguments.

It is important to note that a CSV file is a comma-separated value file. Usually, CSV files are generated from spreadsheet-like software such as MS Excel. Regarding the file type CSV files are very similar to txt files, however, CSV files can be easily opened in MS Excel. The read.csv() function imports the CSV file as a data frame in R Language, a fundamental data structure in R.

read.csv Function in R

Using the read.csv function in R one can read the data from a CSV file by choosing the file (a dialog box opens to select the appropriate file). This is the easy way to choose a data file as the user does not need to type the file path. For example,

data <- read.csv(file.choose(),header =TRUE)

The file.choose() argument will open a dialog box for the selection of the required file.

Read Data from CSV File

After selecting the data file, one can use the data and may display and get the data information, such as

head(data)
str(data)

There is another way to read the data by giving the complete path to the file with the data file name and its extension. The read.csv function in R can be used with important arguments, such as file path and header=TRUE.

data <- read.csv("C:\\book1.csv", header=TRUE)
data <- read.csv("C:\\mywork\\data\\book1.csv", header=TRUE)

After reading the data file, one can check the names of each variable by using names() function.

names(data)

Selecting Variables from Data Object

One can select a column (variable) by using square brackets and column index or by use of a dollar sign. For example

data$X1    # Selects the variable X1
data[, 1]  # selects the variable in column 1
data[, 4]  # selects the variable in column 4
data[, 1:3] # selects column 1, 2 and 3 

Similarly, one can also select the rows from a data file. For example

data[12, ]   # select the 12 observation/ row of all variables (columns)
data[5:10, ] # selects rows 5 to 10 with all columns/variables

One can also subset the data by using some conditional operator. For example, the following command reads $X_1$ variable from data having greater than 0.7 values.

data1[data1$X1 > 0.7, ]

Read a CSV File as a Table

One can also read a CSV file as a table. For example,

data <- read.table("C:\\data.csv",sep ",",header True)

Some important arguments related to read.csv() function:

  • file: The file argument is used to specify the path to the CSV file. One can provide either the absolute path (e.g., “C:/Users/yourname/Documents/data.csv”) or the relative path if the file is in the working directory.
  • header (optional): The header argument is logical (either TRUE or FALSE), it indicates whether the first row of the CSV file contains names of the columns. By default, header=TRUE. In case, if the file does not have a header row, set it header=FALSE.
  • sep (optional): The sep argument specifies the delimiter (separator) used between values in the CSV file. The default is a comma (“,”).
  • dec (optional): The dec argument defines the decimal point character used in the CSV file. The default is “.”.
RFAQS.com Read Data From CSV File

https://itfeature.com

https://gmstat.com

Important Data Frame Questions (2024)

The post contains Data frame Questions and Answers. A data frame in R is a fundamental data structure used to store and organize tabular data. A Data Frame is like a spreadsheet with rows and columns, but more flexible in data types.

Merging Data Frames inR

Question 1: How two data frames can be merged in R language?

Answer: Data frames in the R language can be merged manually using the column bind function cbind() or by using the merge() function on common rows or columns.

Question 2: What is the difference between a data frame and a matrix in R?

Answer: A Data frame can contain heterogeneous inputs while a matrix cannot. In a matrix only similar data types (say either numeric or symbols) can be stored whereas in a data frame, there can be different data types like characters, integers, or other data frames. In short columns of a matrix have the same data type while different columns of a data frame can have different data types.

Dropping Variables Using Indices

Question 3: How will you drop variables using indices in a data frame?

Answer: Consider the data frame the following data frame

df <- data.frame(v1 = c(1:5),
                 v2 = c(2:6),
                 v3 = c(3:7),
                 v4 = c(4:8))
df

# output
  v1 v2 v3 v4
1  1  2  3  4
2  2  3  4  5
3  3  4  5  6
4  4  5  6  7
5  5  6  7  8
Data Frame Questions and Answers

Suppose we want to drop variables $v2$ & $v3$, the variables $v2$ and $v3$ can be dropped using negative indicies as follows:

df1 <- df[-c(2, 3)]
df1

#output
  v1 v4
1  1  4
2  2  5
3  3  6
4  4  7
5  5  8

One can do the same by using the positive indexes.

df2 <- df[c(1, 4)]
df2

#output
  v1 v4
1  1  4
2  2  5
3  3  6
4  4  7
5  5  8

Merging Data Frame in R Language

Question 4: How two Data Frames can be merged in the R programming language?

Answer: The merge() function in R is used to combine two data frames and it identifies common rows or columns between the 2 data frames. The merge() function finds the intersection between two different sets of data. The merge() function in R language takes a long list of arguments as follows

The syntax for using the merge() function in R language:

 merge (x, y, by.x, by.y, all.x  or all.y or all )
  • $X$ represents the first data frame.
  • $Y$ represents the second data frame.
  • $by.X$ Variable name in dataframe $X$ that is common in $Y$.
  • $by.Y$ Variable name in dataframe $Y$ that is common in $X$.
  • $all.x$ It is a logical value that specifies the type of merge. The $all.X$ should be set to TRUE if we want all the observations from data frame $X$. This results in Left Join.
  • $all.y$ It is a logical value that specifies the type of merge. The $all.y$ should be set to TRUE if we want all the observations from data frame $Y$. This results in Right Join.
  • $all$ The default value for this is set to FALSE which means that only matching rows are returned resulting in an Inner join. This should be set to true if you want all the observations from data frame $X$ and $Y$ resulting in Outer join.

Question 5: What is the process to create a table in R language without using external files?

Answer:

MyTable = data.frame()
edit(MyTable)
Data Frame Questions Data Editor in R

The above code will open an Excel Spreadsheet for entering data into MyTable.

Read more about “R FAQ about Data Frame“.

https://itfeature.com