R-Programming Practice Flashcards

1
Q

How to create a data frame manually ?

A
id = c('A','B','C','D')
age = c(45,22,30,35)
height = c(165,130,145,140)
studentData = data.frame(id,age,height)
rownames(studentData) = c('Jhon','Bob','Danny','Ben')
colnames = c('ID','Age','Height')
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How to create data frame from csv file ?

A
file = 'mtcars2.csv'
carData = read.csv(file , header = TRUE , row.names = 1 , stringsAsFactors = FALSE)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How to extract all row names of a data frame and store it in a variable ?

A
file = "mtcars2.csv"
carData = read.csv(file, header = TRUE , row.names = 1 , stringsAsFactors = FALSE)
rowNames = rownames(carData)
print(rowNames)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How to extract all columns of data frame and store it in Variable ?

A
file = "mtcars2.csv"
carData = read.csv(file, header = TRUE , row.names = 1 , stringsAsFactors = FALSE)
colNames = colnames(carData)
print(colNames)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to get the factors of data frame rows only?

A

factor(rownames(carData))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How to get the factor of data frame column ?

A
factor(colnames(carData))
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How to get the factors of data column and compare the factors and levels ?

A
factor(carData$mpg)
length(factor(carData$mpg))
length(levels(factor(carData$mpg)))
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Find out the typeof and class of vector in the list ?
my_list=list(1,2,3,'Jhon',c('A','B','C','D'))

A
my_list=list(1,2,3,'Jhon',c('A','B','C','D'))
typeof(my_list)
class(my_list
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Create a list and define the slots ?

A
factor(colnames(carData))
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Create a matrix and give the column names and row names ?

A

myMatrix = matrix(c(1,2,3,4,5,6,7,8,9) , nrow = 3, ncol = 3 , byrow = TRUE)
rownames(myMatrix) = c(‘Row1’,’Row2’,’Row3’)
colnames(myMatrix) = c(‘Col1’,’Col2’,’Col3’)
myMatrix

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How to extract a Single colum with header and row names ?

A
studentData['Age']
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Write a code which clearly demonstrate the difference between class and typeof of method ?

A
file = 'mtcars2.csv'
carData = read.csv(file , header = TRUE , row.names = 1 , stringsAsFactors = FALSE)

head(carData,2)

rowNames = rownames(carData)
print(rowNames)

str(rowNames)
class(rowNames)
typeof(rowNames)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly