R-Programming Practice Flashcards
How to create a data frame manually ?
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 to create data frame from csv file ?
file = 'mtcars2.csv' carData = read.csv(file , header = TRUE , row.names = 1 , stringsAsFactors = FALSE)
How to extract all row names of a data frame and store it in a variable ?
file = "mtcars2.csv" carData = read.csv(file, header = TRUE , row.names = 1 , stringsAsFactors = FALSE) rowNames = rownames(carData) print(rowNames)
How to extract all columns of data frame and store it in Variable ?
file = "mtcars2.csv" carData = read.csv(file, header = TRUE , row.names = 1 , stringsAsFactors = FALSE) colNames = colnames(carData) print(colNames)
How to get the factors of data frame rows only?
factor(rownames(carData))
How to get the factor of data frame column ?
factor(colnames(carData))
How to get the factors of data column and compare the factors and levels ?
factor(carData$mpg) length(factor(carData$mpg)) length(levels(factor(carData$mpg)))
Find out the typeof and class of vector in the list ?my_list=list(1,2,3,'Jhon',c('A','B','C','D'))
my_list=list(1,2,3,'Jhon',c('A','B','C','D')) typeof(my_list) class(my_list
Create a list and define the slots ?
factor(colnames(carData))
Create a matrix and give the column names and row names ?
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 to extract a Single colum with header and row names ?
studentData['Age']
Write a code which clearly demonstrate the difference between class and typeof of method ?
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)