Laboratory of Microbial Genomics and Big Data (강원대학교 미생물유전체빅데이터 연구실)

R: Basics - Vector, Matrix, Data Frame, List, Factor - by Eun Bae Kim (08/22/2018)
 Visits : 497,912 ( Your IP 3.23.101.241 )
 

 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: 
 27: 
 28: 
 29: 
 30: 
 31: 
 32: 
 33: 
 34: 
 35: 
 36: 
 37: 
 38: 
 39: 
 40: 
 41: 
 42: 
 43: 
 44: 
 45: 
 46: 
 47: 
 48: 
 49: 
 50: 
 51: 
 52: 
 53: 
 54: 
 55: 
 56: 
 57: 
 58: 
 59: 
 60: 
 61: 
 62: 
 63: 
 64: 
 65: 
 66: 
 67: 
 68: 
 69: 
 70: 
 71: 
 72: 
 73: 
 74: 
 75: 
 76: 
 77: 
 78: 
 79: 
 80: 
 81: 
 82: 
 83: 
 84: 
 85: 
 86: 
 87: 
 88: 
 89: 
 90: 
 91: 
 92: 
 93: 
 94: 
 95: 
 96: 
 97: 
 98: 
 99: 
 100: 
 101: 
 102: 
 103: 
 104: 
 105: 
 106: 
 107: 
 108: 
 109: 
 110: 
 111: 
 112: 
 113: 
 114: 
 115: 
 116: 
 117: 
 118: 
 119: 
 120: 
 121: 
 122: 
 123: 
 124: 
 125: 
 126: 
 127: 
 128: 
 129: 
 130: 
 131: 
 132: 
 133: 
 134: 
 135: 
 136: 
 137: 
 138: 
 139: 
 140: 
 141: 
 142: 
 143: 
 144: 
 145: 
 146: 
 147: 
 148: 
 149: 
 150: 
 151: 
 152: 
 153: 
 154: 
# For R Basics ###################################################################
3 + 4; 3^2
a = 5; a
a + b
b <- 5; b
c = 1:10; c
d = rep("abc", 10); d
e = "Hello"; e
f = rep(e, a); f
g = factorial(a); g
h = paste(e, ", Mr. Kim"); h
i = NA;   i; is.na(i); is.null(i); # NA = Missing Value ("Not Available")
j = NULL; j; is.na(j); is.null(j); # NULL = Undefined Value



# For Vectors ###################################################################

# Numeric Vector
numKoreanScore       = c(80.5, 90.1, 80.0, 55.3, 100,  98)
numEnglishScore      = c(78,   88.4, 67.5, 33.3, 95,   92)
numMathematicsScore  = c(56,   90,   78.5, 43.8, 98.1, 100)

# Character Vector
chrSubjects = c("Korean", "English", "Mathematics")

# Logical Vector
logicData = c(TRUE, FALSE, TRUE, TRUE, FALSE, TRUE, FALSE)

# Mixed Vector --> All changed to Charater Values
mixData = c(1, 3, -0.5, "English", TRUE); mixData



numKoreanScore
numKoreanScore[1]; numKoreanScore[3:5]; numKoreanScore[c(3, 5)]
is.vector(numKoreanScore); is.na(numKoreanScore); is.null(numKoreanScore);

is.numeric(numKoreanScore); is.numeric(chrSubjects); is.numeric(logicData);
is.character(numKoreanScore); is.character(chrSubjects); is.character(logicData);
is.logical(numKoreanScore); is.logical(chrSubjects); is.logical(logicData);

mean(numKoreanScore);      sd(numKoreanScore);      length(numKoreanScore)
mean(numEnglishScore);     sd(numEnglishScore);     length(numEnglishScore)
mean(numMathematicsScore); sd(numMathematicsScore); length(numMathematicsScore)




# For Matrix #####################################################################
numKoreanScore       = c(80.5, 90.1, 80.0, 55.3, 100,  98)
numEnglishScore      = c(78,   88.4, 67.5, 33.3, 95,   92)
numMathematicsScore  = c(56,   90,   78.5, 43.8, 98.1, 100)
txtNames             = c("EBKim", "GDJin", "JBPark", "IHYou", "JHWon", "SJChoi")

# Binding different type of vectors to make a matrix variable.
tableScores = cbind(txtNames, numKoreanScore, numEnglishScore, numMathematicsScore)
tableScores       # All values were character values.

# Different way
tableScores = cbind(numKoreanScore, numEnglishScore, numMathematicsScore)
rownames(tableScores) = txtNames
tableScores


# Changing Column Names
txtColNames = c("Korean", "English", "Math")
colnames(tableScores) = txtColNames
tableScores

# Making a matrix using a single vector
numData = 1:96
matrixNew = matrix(numData, ncol=12); matrixNew     # Filed by Columns
matrixNew = matrix(numData, nrow=8);  matrixNew     # Filed by Columns

matrixNew = matrix(numData, ncol=12, byrow=TRUE); matrixNew     # Filed by Rows

# With row names and col names
rowNames = paste("Gene", 1:8, sep=""); rowNames
colNames = paste("Sample", 1:12, sep=""); colNames
matrixNew = matrix(numData, ncol=12, byrow=TRUE, dimnames=list(rowNames, colNames)); matrixNew

# get values and set values
matrixNew[4, 5]
matrixNew[4, 5] = 999; matrixNew[4, 5]
matrixNew[4, 1:5]
matrixNew[4, ]
matrixNew[,5]




# For Data Frame ##################################################################
numKoreanScore       = c(80.5, 90.1, 80.0, 55.3, 100,  98)
numEnglishScore      = c(78,   88.4, 67.5, 33.3, 95,   92)
numMathematicsScore  = c(56,   90,   78.5, 43.8, 98.1, 100)
txtNames             = c("EBKim", "GDJin", "JBPark", "IHYou", "JHWon", "SJChoi")

dfData = data.frame(txtNames, numKoreanScore, numEnglishScore, numMathematicsScore)
dfData   # Different data types are included within the same data frame object. 
colnames(dfData) = c("Names", "Korean", "English", "Math")
dfData

# Subset of the data
dfData[,"Names"]
dfData[,1]
dfData["Names"]
dfData[1]
dfData[c("Names", "Math")]
dfData[c(1,4)]
dfData$Names
paste("Hello, ", dfData$Names, sep="")





# For List Object ##################################################################
numEng1 = c(78, 88.4, 67.5, 33.3, 95, 92)
numData1 = 1:36; matNew1 = matrix(numData1, ncol=12)

listData1 = list(txtClassTeacher="EBKim", numEngScores = numEng1, 
                 matrixData = matNew1, numTeacherHeight = 180.2)
listData1
listData1$txtClassTeacher

numEng2 = c(48, 39, 98, 64, 78)
numData2 = 1:48; matNew2 = matrix(numData2, ncol=12)

listData2 = list(txtClassTeacher="GDHong", numEngScores = numEng2, 
                 matrixData = matNew2, numTeacherHeight = 175.4)
# list of lists
listAll = c(listData1, listData2)
listAll
listAll[2]
listAll[5]
listAll$txtClassTeacher
listAll$numEngScores
listAll$numEngScores[3]
listAll[["txtClassTeacher"]]





# For Factors ##################################################################
txtGender = c("Female", "Male", "Female", "Male", "Female", 
              "Female", "Female", "Female", "Male");
txtGender
summary(txtGender)

factorGender = factor(txtGender)
factorGender
summary(factorGender)   # sorted by the Alphabetical order



Kangwon National University