%3CLINGO-SUB%20id%3D%22lingo-sub-379071%22%20slang%3D%22en-US%22%3EMatrices%2C%20Data%20Frames%2C%20Functions%2C%20Conditionals%2C%20Loops%20with%20R%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-379071%22%20slang%3D%22en-US%22%3E%0A%20%26lt%3Bmeta%20http-equiv%3D%22Content-Type%22%20content%3D%22text%2Fhtml%3B%20charset%3DUTF-8%22%20%2F%26gt%3B%3CSTRONG%3E%20First%20published%20on%20MSDN%20on%20Jul%2025%2C%202017%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Guest%20post%20by%20%3CA%20href%3D%22https%3A%2F%2Fwww.linkedin.com%2Fin%2Fslaviana-pavlovich%2F%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%20Slaviana%20Pavlovich%20%3C%2FA%3E%20Microsoft%20Student%20Partner%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F94767i3EC498B37A8FD0C0%22%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20My%20name%20is%20Slaviana%20Pavlovich.%20I%20am%20an%20IT%20and%20Management%20student%20at%20University%20College%20London%20with%20a%20passion%20for%20data%20science.%20I%20recently%20completed%20the%20Microsoft%20Professional%20Program%20for%20Data%20Science%2C%20where%20I%20developed%20core%20skills%20to%20work%20with%20data.%20If%20you%20are%20also%20interested%20in%20this%20career%2C%20but%20not%20sure%20where%20to%20start%20-%20I%20strongly%20encourage%20you%20to%20check%20it%20out.%20I%20also%20have%20a%20wide%20range%20of%20interests%20including%203D%20bioprinting%2C%20public%20speaking%2C%20and%20politics.%20Additionally%2C%20I%20enjoy%20swimming%20and%20photography%20to%20balance%20out%20my%20studies.%20I%20became%20a%20Microsoft%20Student%20Partner%20at%20the%20end%20of%20my%20first%20year%20and%20I%20absolutely%20enjoy%20being%20part%20of%20such%20a%20vibrant%20community.%20If%20you%20have%20any%20questions%2C%20feel%20free%20to%20ask!%20%3CBR%20%2F%3E%3CH3%20id%3D%22toc-hId-1483429780%22%20id%3D%22toc-hId-1081707379%22%3EIntroduction%3C%2FH3%3E%3CBR%20%2F%3E%20In%20today%E2%80%99s%20article%2C%20I%20am%20going%20to%20continue%20talking%20about%20R.%20In%20the%20second%20part%20of%20this%20two-part%20introduction%20to%20R%20(the%20first%20part%20is%20available%20%3CA%20href%3D%22https%3A%2F%2Fblogs.msdn.microsoft.com%2Fuk_faculty_connection%2F2017%2F07%2F17%2Fgetting-started-with-r%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3E%20here%20%3C%2FA%3E%20)%2C%20we%20are%20going%20to%20consider%3A%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%E2%80%A2%20Matrices%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%E2%80%A2%20Data%20Frames%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%E2%80%A2%20Functions%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%E2%80%A2%20Conditionals%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%E2%80%A2%20Loops%20%3CBR%20%2F%3E%3CH3%20id%3D%22toc-hId--1068727181%22%20id%3D%22toc-hId--725747084%22%3EMatrices%3C%2FH3%3E%3CBR%20%2F%3E%20Matrix%20is%20another%20data%20type%20that%20we%20are%20going%20to%20look%20at.%20Matrix%20is%20a%20two-dimensional%20data%20set.%20A%20matrix%20is%20created%20using%20the%20function%20%3CI%3E%20matrix()%20%3C%2FI%3E%20%3A%20%3CBR%20%2F%3E%3CDIV%20id%3D%22codeSnippetWrapper%22%3E%3CBR%20%2F%3E%3CDIV%20id%3D%22codeSnippet%22%3E%3CBR%20%2F%3E%20%26gt%3B%20%23%20creating%20a%20matrix%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%26gt%3B%20example%20%26lt%3B-%20matrix(c(99%2C45%2C4%2C47%2C2%2C5)%2C%20nrow%20%3D%203%2C%20ncol%20%3D%202%2C%20byrow%20%3D%20TRUE)%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%26gt%3B%20example%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B%2C1%5D%20%5B%2C2%5D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B1%2C%5D%2099%2045%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B2%2C%5D%204%2047%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B3%2C%5D%202%205%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3C%2FDIV%3E%3CBR%20%2F%3E%20As%20you%20can%20see%20in%20the%20example%20above%2C%20%3CI%3E%20nrow%20%3C%2FI%3E%20and%20%3CI%3E%20ncol%20%3C%2FI%3E%20are%20used%20to%20define%20the%20values%20for%20rows%20and%20columns.%20Also%2C%20%3CI%3E%20byrow%20%3D%20TRUE%20%3C%2FI%3E%20means%20that%20the%20matrix%20is%20filled%20by%20rows%2C%20while%20%3CI%3E%20byrow%3DFALSE%20%3C%2FI%3E%20%E2%80%93%20by%20columns.%20Let%E2%80%99s%20look%20at%20the%20following%20example%3A%20%3CBR%20%2F%3E%3CDIV%20id%3D%22codeSnippetWrapper%22%3E%3CBR%20%2F%3E%3CDIV%20id%3D%22codeSnippet%22%3E%3CBR%20%2F%3E%20%26gt%3B%20%23%20creating%20a%202x3%20matrix%20that%20contains%20the%20numbers%20from%201%20to%206%20and%20filled%20by%20columns%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%26gt%3B%20example.2%20%26lt%3B-%20matrix(1%3A6%2C%20nrow%20%3D%202%2C%20ncol%20%3D%203%2C%20byrow%20%3D%20FALSE)%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%26gt%3B%20example.2%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B%2C1%5D%20%5B%2C2%5D%20%5B%2C3%5D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B1%2C%5D%201%203%205%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B2%2C%5D%202%204%206%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3C%2FDIV%3E%3CBR%20%2F%3E%20To%20change%20the%20names%20of%20rows%20and%20columns%20of%20the%20matrix%20use%20%3CI%3E%20dimnames%20%3C%2FI%3E%20%3A%20%3CBR%20%2F%3E%3CDIV%20id%3D%22codeSnippetWrapper%22%3E%3CBR%20%2F%3E%3CDIV%20id%3D%22codeSnippet%22%3E%3CBR%20%2F%3E%20%26gt%3B%20%23%20creating%20a%20matrix%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%26gt%3B%20A%20%26lt%3B-%20matrix(1%3A6%2C%20nrow%20%3D%203%2C%20byrow%20%3D%20TRUE)%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%26gt%3B%20%23%20setting%20row%20and%20column%20names%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%26gt%3B%20A%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B%2C1%5D%20%5B%2C2%5D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B1%2C%5D%201%202%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B2%2C%5D%203%204%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B3%2C%5D%205%206%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%26gt%3B%20dimnames(A)%20%3D%20list(c(%221row%22%2C%20%222row%22%2C%20%223row%22)%2C%20c(%221col%22%2C%20%222col%22))%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%26gt%3B%20A%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%201col%202col%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%201row%201%202%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%202row%203%204%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%203row%205%206%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3C%2FDIV%3E%3CBR%20%2F%3E%20There%20are%20certain%20operations%20you%20can%20do%20with%20matrices.%20You%20can%20transpose%20a%20matrix%2C%20using%20a%20function%20%3CI%3E%20t()%20%3C%2FI%3E%20%3A%20%3CBR%20%2F%3E%3CDIV%20id%3D%22codeSnippetWrapper%22%3E%3CBR%20%2F%3E%3CDIV%20id%3D%22codeSnippet%22%3E%3CBR%20%2F%3E%20%26gt%3B%20M%20%26lt%3B-%20matrix(c(14%2C2%2C4%2C3%2C2%2C5)%2C%20nrow%20%3D%202%2C%20ncol%20%3D%203%2C%20byrow%20%3D%20TRUE)%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%26gt%3B%20M%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B%2C1%5D%20%5B%2C2%5D%20%5B%2C3%5D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B1%2C%5D%2014%202%204%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B2%2C%5D%203%202%205%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%26gt%3B%20t(M)%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B%2C1%5D%20%5B%2C2%5D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B1%2C%5D%2014%203%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B2%2C%5D%202%202%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B3%2C%5D%204%205%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3C%2FDIV%3E%3CBR%20%2F%3E%20Furthermore%2C%20use%20%3CI%3E%20solve()%20%3C%2FI%3E%20function%20to%20find%20an%20inverse%20of%20a%20square%20matrix%3A%20%3CBR%20%2F%3E%3CDIV%20id%3D%22codeSnippetWrapper%22%3E%3CBR%20%2F%3E%3CDIV%20id%3D%22codeSnippet%22%3E%3CBR%20%2F%3E%20%26gt%3B%20X%20%26lt%3B-%20matrix(1%3A4%2C%20nrow%20%3D%202%2C%20byrow%20%3D%20TRUE)%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%26gt%3B%20X%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B%2C1%5D%20%5B%2C2%5D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B1%2C%5D%201%202%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B2%2C%5D%203%204%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%26gt%3B%20solve(X)%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B%2C1%5D%20%5B%2C2%5D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B1%2C%5D%20-2.0%201.0%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B2%2C%5D%201.5%20-0.5%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3C%2FDIV%3E%3CBR%20%2F%3E%20Arithmetic%20operations%20are%20done%20element-wise%3A%20%3CBR%20%2F%3E%3CDIV%20id%3D%22codeSnippetWrapper%22%3E%3CBR%20%2F%3E%3CDIV%20id%3D%22codeSnippet%22%3E%3CBR%20%2F%3E%20%26gt%3B%20A%20%26lt%3B-%20matrix(1%3A6%2C%20nrow%20%3D%203%2C%20byrow%20%3D%20TRUE)%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%26gt%3B%20B%20%26lt%3B-%20matrix(1%3A6%2C%20nrow%20%3D%202%2C%20byrow%20%3D%20TRUE)%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%26gt%3B%20A%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B%2C1%5D%20%5B%2C2%5D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B1%2C%5D%201%202%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B2%2C%5D%203%204%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B3%2C%5D%205%206%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%26gt%3B%20B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B%2C1%5D%20%5B%2C2%5D%20%5B%2C3%5D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B1%2C%5D%201%202%203%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B2%2C%5D%204%205%206%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%26gt%3B%20A%20%2B%202%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B%2C1%5D%20%5B%2C2%5D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B1%2C%5D%203%204%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B2%2C%5D%205%206%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B3%2C%5D%207%208%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%26gt%3B%20B%20%2F%202%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B%2C1%5D%20%5B%2C2%5D%20%5B%2C3%5D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B1%2C%5D%200.5%201.0%201.5%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B2%2C%5D%202.0%202.5%203.0%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3C%2FDIV%3E%3CBR%20%2F%3E%20For%20matrix%20multiplication%20use%20%E2%80%9C%25*%25%E2%80%9D%3A%20%3CBR%20%2F%3E%3CDIV%20id%3D%22codeSnippetWrapper%22%3E%3CBR%20%2F%3E%3CDIV%20id%3D%22codeSnippet%22%3E%3CBR%20%2F%3E%20%26gt%3B%20A%20%25*%25%20B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B%2C1%5D%20%5B%2C2%5D%20%5B%2C3%5D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B1%2C%5D%209%2012%2015%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B2%2C%5D%2019%2026%2033%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B3%2C%5D%2029%2040%2051%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3C%2FDIV%3E%3CBR%20%2F%3E%20In%20R%2C%20to%20select%20elements%20of%20the%20matrix%2C%20do%20the%20following%3A%20%3CBR%20%2F%3E%3CDIV%20id%3D%22codeSnippetWrapper%22%3E%3CBR%20%2F%3E%3CDIV%20id%3D%22codeSnippet%22%3E%3CBR%20%2F%3E%20%26gt%3B%20K%20%26lt%3B-%20matrix(4%3A7%2C%20nrow%20%3D%202%2C%20byrow%20%3D%20TRUE)%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%26gt%3B%20K%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B%2C1%5D%20%5B%2C2%5D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B1%2C%5D%204%205%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B2%2C%5D%206%207%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%26gt%3B%20K%5B1%2C2%5D%20%23%20element%20at%201st%20row%20and%202rd%20column%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B1%5D%205%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%26gt%3B%20K%5B1%2C%5D%20%23%20first%20row%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B1%5D%204%205%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%26gt%3B%20K%5B%2C2%5D%20%23%20second%20column%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B1%5D%205%207%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3C%2FDIV%3E%3CBR%20%2F%3E%20Finally%2C%20we%20can%20always%20modify%20a%20matrix%3A%20%3CBR%20%2F%3E%3CDIV%20id%3D%22codeSnippetWrapper%22%3E%3CBR%20%2F%3E%3CDIV%20id%3D%22codeSnippet%22%3E%3CBR%20%2F%3E%20%26gt%3B%20V%20%26lt%3B-%20matrix(1%3A9%2C%20ncol%20%3D%203)%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%26gt%3B%20V%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B%2C1%5D%20%5B%2C2%5D%20%5B%2C3%5D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B1%2C%5D%201%204%207%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B2%2C%5D%202%205%208%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B3%2C%5D%203%206%209%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%26gt%3B%20V%5B1%2C3%5D%20%26lt%3B-%200%3B%20V%20%23%20modify%20a%20single%20element%20at%201st%20row%20and%203rd%20column%20to%200%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B%2C1%5D%20%5B%2C2%5D%20%5B%2C3%5D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B1%2C%5D%201%204%200%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B2%2C%5D%202%205%208%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B3%2C%5D%203%206%209%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%26gt%3B%20V%5BV%26gt%3B2%5D%20%26lt%3B-%201%3B%20V%20%23%20change%20all%20elements%20greater%20than%202%20to%201%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B%2C1%5D%20%5B%2C2%5D%20%5B%2C3%5D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B1%2C%5D%201%201%200%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B2%2C%5D%202%201%201%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B3%2C%5D%201%201%201%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3CH3%20id%3D%22toc-hId-674083154%22%20id%3D%22toc-hId-1761765749%22%3EData%20Frames%3C%2FH3%3E%3CBR%20%2F%3E%20After%20looking%20at%20matrices%2C%20I%20suggest%20learning%20about%20data%20frames.%20A%20data%20frame%20is%20a%20special%20case%20of%20a%20list%20(another%20data%20object%20in%20R%20that%20was%20previously%20considered%20in%20%3CA%20href%3D%22https%3A%2F%2Fblogs.msdn.microsoft.com%2Fuk_faculty_connection%2F2017%2F07%2F17%2Fgetting-started-with-r%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3E%20the%20first%20part%20of%20the%20article%20%3C%2FA%3E%20).%20Data%20frames%20are%20used%20for%20storing%20tables.%20Unlike%20matrices%2C%20each%20column%2C%20also%20known%20as%20a%20vector%2C%20can%20store%20different%20types%20of%20data%20(logical%2C%20numeric%2C%20character%2C%20complex%2C%20etc.).%20The%20function%20%3CI%3E%20data.frame()%20%3C%2FI%3E%20is%20used%20to%20create%20a%20data%20frame%3A%20%3CBR%20%2F%3E%3CDIV%20id%3D%22codeSnippetWrapper%22%3E%3CBR%20%2F%3E%3CDIV%20id%3D%22codeSnippet%22%3E%3CBR%20%2F%3E%20%26gt%3B%20%23%20creating%20a%20data%20frame%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%26gt%3B%20table%20%26lt%3B-%20data.frame(name%3Dc(%22Jack%22%2C%20%22Karan%22%2C%20%22Thomas%22%2C%20%22Vito%22%2C%20%22Kristine%22)%2C%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%2B%20age%3Dc(19%2C%2020%2C%2019%2C%2019%2C%2019)%2C%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%2B%20sex%3Dc(%22M%22%2C%20%22M%22%2C%20%22M%22%2C%20%22M%22%2C%20%22F%22)%2C%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%2B%20colour%3Dc(%22yellow%22%2C%20%22red%22%2C%20%22green%22%2C%20%22blue%22%2C%20%22pink%22))%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%26gt%3B%20table%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20name%20age%20sex%20colour%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%201%20Jack%2019%20M%20yellow%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%202%20Karan%2020%20M%20red%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%203%20Thomas%2019%20M%20green%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%204%20Vito%2019%20M%20blue%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%205%20Kristine%2019%20F%20pink%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%26gt%3B%20typeof(table)%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B1%5D%20%22list%22%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%26gt%3B%20class(table)%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B1%5D%20%22data.frame%22%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%26gt%3B%20%23%20function%20of%20a%20data%20frame%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%26gt%3B%20names(table)%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B1%5D%20%22name%22%20%22age%22%20%22sex%22%20%22colour%22%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%26gt%3B%20nrow(table)%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B1%5D%205%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%26gt%3B%20ncol(table)%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B1%5D%204%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3C%2FDIV%3E%3CBR%20%2F%3E%20There%20are%20several%20ways%20of%20accessing%20an%20element%20of%20a%20data%20frame%3A%20%3CBR%20%2F%3E%3CDIV%20id%3D%22codeSnippetWrapper%22%3E%3CBR%20%2F%3E%3CDIV%20id%3D%22codeSnippet%22%3E%3CBR%20%2F%3E%20%26gt%3B%20table%5B2%3A4%5D%20%23%20columns%20starting%20from%202nd%20to%204th%20of%20data%20frame%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20age%20sex%20colour%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%201%2019%20M%20yellow%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%202%2020%20M%20red%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%203%2019%20M%20green%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%204%2019%20M%20blue%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%205%2019%20F%20pink%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%26gt%3B%20table%5Bc(%22colour%22%2C%22age%22)%5D%20%23%20columns%20with%20the%20titles%20colour%20and%20age%20from%20data%20frame%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20colour%20age%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%201%20yellow%2019%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%202%20red%2020%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%203%20green%2019%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%204%20blue%2019%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%205%20pink%2019%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3C%2FDIV%3E%3CBR%20%2F%3E%20In%20a%20similar%20way%20to%20matrices%2C%20it%20is%20possible%20to%20change%20the%20values%20of%20the%20elements%3A%20%3CBR%20%2F%3E%3CDIV%20id%3D%22codeSnippetWrapper%22%3E%3CBR%20%2F%3E%3CDIV%20id%3D%22codeSnippet%22%3E%3CBR%20%2F%3E%20%26gt%3B%20table%5B3%2C%22age%22%5D%20%26lt%3B-%2020%3B%20table%20%23%20modify%20the%20element%20at%203st%20row%20and%20column%20age%20to%2020%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20name%20age%20sex%20colour%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%201%20Jack%2019%20M%20yellow%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%202%20Karan%2020%20M%20red%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%203%20Thomas%2020%20M%20green%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%204%20Vito%2019%20M%20blue%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%205%20Kristine%2019%20F%20pink%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3CH3%20id%3D%22toc-hId--1878073807%22%20id%3D%22toc-hId--45688714%22%3EFunctions%3C%2FH3%3E%3CBR%20%2F%3E%20There%20is%20a%20straightforward%20way%20of%20creating%20own%20functions%20in%20R.%20Let%E2%80%99s%20consider%20an%20example%20where%20our%20function%20is%20going%20to%20find%20the%20difference%20between%20two%20integers%3A%20%3CBR%20%2F%3E%3CDIV%20id%3D%22codeSnippetWrapper%22%3E%3CBR%20%2F%3E%3CDIV%20id%3D%22codeSnippet%22%3E%3CBR%20%2F%3E%20%26gt%3B%20example%20%26lt%3B-%20function%20(a%2C%20b)%20%7B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%2B%20c%20%26lt%3B-%20a%20-%20b%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%2B%20c%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%2B%20%7D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%26gt%3B%20example(15%2C%201)%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B1%5D%2014%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3C%2FDIV%3E%3CBR%20%2F%3E%20As%20shown%20above%2C%20the%20word%20%3CI%3E%20function%20%3C%2FI%3E%20is%20used%20to%20declare%20a%20function%20in%20R.%20Now%20we%20are%20going%20to%20create%20a%20function%20that%20prints%20a%20type%20and%20class%20of%20an%20argument%3A%20%3CBR%20%2F%3E%3CDIV%20id%3D%22codeSnippetWrapper%22%3E%3CBR%20%2F%3E%3CDIV%20id%3D%22codeSnippet%22%3E%3CBR%20%2F%3E%20example%26lt%3B-function(X)%7B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%2B%20print(typeof(X))%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%2B%20print(class(X))%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%26gt%3B%20example%20%26lt%3B-%20function%20(a%2C%20b)%20%7B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%2B%20c%20%26lt%3B-%20a%20-%20b%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%2B%20c%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%2B%20%7D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%26gt%3B%20example(15%2C%201)%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B1%5D%2014%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%2B%20print(paste(%22The%20type%20is%22%2C%20typeof(X)%20%2C%20%22and%20class%20is%22%2C%20class(X)))%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%2B%20%7D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%26gt%3B%20Y%26lt%3B-c(%22Vito%22)%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%26gt%3B%20example(Y)%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B1%5D%20%22character%22%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B1%5D%20%22character%22%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B1%5D%20%22The%20type%20is%20character%20and%20class%20is%20character%22%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%26gt%3B%20Z%26lt%3B-c(11)%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%26gt%3B%20example(Z)%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B1%5D%20%22double%22%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B1%5D%20%22numeric%22%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B1%5D%20%22The%20type%20is%20double%20and%20class%20is%20numeric%22%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3C%2FDIV%3E%3CBR%20%2F%3E%20If%20you%20want%20to%20take%20an%20input%20from%20the%20user%2C%20use%20the%20function%20%3CI%3E%20readline()%20%3C%2FI%3E%20in%20R%3A%20%3CBR%20%2F%3E%3CDIV%20id%3D%22codeSnippetWrapper%22%3E%3CBR%20%2F%3E%3CDIV%20id%3D%22codeSnippet%22%3E%3CBR%20%2F%3E%20read.example%20%26lt%3B-%20function()%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%2B%20%7B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%2B%20str%20%26lt%3B-%20readline(prompt%3D%22Your%20name%3A%20%22)%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%2B%20return(as.character(str))%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%2B%20%7D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%26gt%3B%20print(paste(%22Nice%20to%20meet%20you%2C%22%2C%20read.example()%2C%20%22!%22))%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Your%20name%3A%20Dre%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B1%5D%20%22Nice%20to%20meet%20you%2C%20Dre%20!%22%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3CH3%20id%3D%22toc-hId--135263472%22%20id%3D%22toc-hId--1853143177%22%3EConditionals%3C%2FH3%3E%3CBR%20%2F%3E%20To%20use%20conditional%20execution%20in%20R%2C%20we%20are%20going%20to%20use%20%3CI%3E%20if%E2%80%A6else%20%3C%2FI%3E%20statement%3A%20%3CBR%20%2F%3E%3CDIV%20id%3D%22codeSnippetWrapper%22%3E%3CBR%20%2F%3E%3CDIV%20id%3D%22codeSnippet%22%3E%3CBR%20%2F%3E%20%26gt%3B%20x%20%26lt%3B-%204%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%26gt%3B%20if%20(x%20%26lt%3B%200)%20%7B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%2B%20print(%22It%20is%20a%20negative%20number!%22)%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%2B%20%7D%20else%20if%20(x%20%26gt%3B%200)%20%7B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%2B%20print(%22It%20is%20a%20positive%20number!%22)%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%2B%20%7D%20else%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%2B%20print(%22Zero!%22)%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B1%5D%20%22It%20is%20a%20positive%20number!%22%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%26gt%3B%20x%20%26lt%3B-%20-10%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%26gt%3B%20if%20(x%20%26lt%3B%200)%20%7B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%2B%20print(%22It%20is%20a%20negative%20number!%22)%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%2B%20%7D%20else%20if%20(x%20%26gt%3B%200)%20%7B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%2B%20print(%22It%20is%20a%20positive%20number!%22)%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%2B%20%7D%20else%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%2B%20print(%22Zero!%22)%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B1%5D%20%22It%20is%20a%20negative%20number!%22%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%26gt%3B%20x%20%26lt%3B-%200%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%26gt%3B%20if%20(x%20%26lt%3B%200)%20%7B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%2B%20print(%22It%20is%20a%20negative%20number!%22)%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%2B%20%7D%20else%20if%20(x%20%26gt%3B%200)%20%7B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%2B%20print(%22It%20is%20a%20positive%20number!%22)%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%2B%20%7D%20else%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%2B%20print(%22Zero!%22)%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B1%5D%20%22Zero!%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3C%2FDIV%3E%3CBR%20%2F%3E%20Loops%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Now%20we%20are%20going%20to%20consider%20the%20control%20statements%20in%20R%2C%20such%20as%20for%7B%7D%2C%20repeat%7B%7D%20and%20while%7B%7D.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%C2%B7%20A%20%3CI%3E%20for%7B%7D%20%3C%2FI%3E%20loop%20in%20the%20example%20below%20is%20going%20to%20print%20the%20first%20three%20numbers%20in%20the%20vector%20Y%3A%20%3CBR%20%2F%3E%3CDIV%20id%3D%22codeSnippetWrapper%22%3E%3CBR%20%2F%3E%3CDIV%20id%3D%22codeSnippet%22%3E%3CBR%20%2F%3E%20%26gt%3B%20Y%20%26lt%3B-%20c(17%2C%2025%2C%2019%2C%2033%2C%2011%2C%2051%2C%2055)%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%26gt%3B%20for(i%20in%201%3A3)%20%7B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%2B%20print(Y%5Bi%5D)%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%2B%20%7D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B1%5D%2017%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B1%5D%2025%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B1%5D%2019%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3C%2FDIV%3E%3CBR%20%2F%3E%20%C2%B7%20In%20this%20example%2C%20a%20%3CI%3E%20repeat%7B%7D%20%3C%2FI%3E%20loop%20is%20going%20to%20print%20%E2%80%9Ctask%E2%80%9D%20and%20after%203%20loops%20it%20is%20going%20to%20break%3A%20%3CBR%20%2F%3E%3CDIV%20id%3D%22codeSnippetWrapper%22%3E%3CBR%20%2F%3E%3CDIV%20id%3D%22codeSnippet%22%3E%3CBR%20%2F%3E%20%26gt%3B%20task%20%26lt%3B-%20c(%22R%20is%20great!%22)%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%26gt%3B%20i%20%26lt%3B-%203%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%26gt%3B%20repeat%20%7B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%2B%20i%20%26lt%3B-%20i%20%2B%201%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%2B%20print(task)%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%2B%20if(i%20%26gt%3B%205)%20%7B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%2B%20break%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%2B%20%7D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%2B%20%7D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B1%5D%20%22R%20is%20great!%22%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B1%5D%20%22R%20is%20great!%22%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B1%5D%20%22R%20is%20great!%22%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3C%2FDIV%3E%3CBR%20%2F%3E%20%C2%B7%20A%20%3CI%3E%20while%7B%7D%20%3C%2FI%3E%20loop%20is%20going%20to%20follow%20the%20commands%20as%20long%20as%20the%20condition%20is%20true%3A%20%3CBR%20%2F%3E%3CDIV%20id%3D%22codeSnippetWrapper%22%3E%3CBR%20%2F%3E%3CDIV%20id%3D%22codeSnippet%22%3E%3CBR%20%2F%3E%20%26gt%3B%20i%20%26lt%3B-%201%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%26gt%3B%20while(i%20%26lt%3B%205)%20%7B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%2B%20print(i)%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%2B%20i%20%26lt%3B-%20i%20%2B%201%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%2B%20%7D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B1%5D%201%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B1%5D%202%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B1%5D%203%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B1%5D%204%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3C%2FDIV%3E%3CBR%20%2F%3E%20Resources%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20There%20are%20so%20many%20interesting%20resources%20online%20that%20can%20help%20you%20further%20with%20R.%20I%20strongly%20recommend%20checking%20them%20out.%20In%20the%20following%20article%2C%20I%20am%20going%20to%20cover%20data%20visualisation%2C%20stay%20updated!%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CA%20href%3D%22https%3A%2F%2Facademy.microsoft.com%2Fen-us%2Fprofessional-program%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3E%20https%3A%2F%2Facademy.microsoft.com%2Fen-us%2Fprofessional-program%2F%20%3C%2FA%3E%20Microsoft%20professional%20programmes%2C%20Big%20Data%2C%20Data%20Science%20%3CBR%20%2F%3E%20%3CA%20href%3D%22https%3A%2F%2Fimagine.microsoft.com%2Fen-us%2FCatalog%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3E%20https%3A%2F%2Fimagine.microsoft.com%2Fen-us%2FCatalog%20%3C%2FA%3E%20R%20Server%20Download%20for%20Students%20%26amp%3B%20Academics%20via%20Imagine%20Access%20%3CBR%20%2F%3E%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fr-server%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3E%20https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fr-server%2F%20%3C%2FA%3E%20R%20Server%20and%20R%20Documentation%20%3CBR%20%2F%3E%20%3CA%20href%3D%22https%3A%2F%2Fwww.microsoft.com%2Fen-gb%2Fcloud-platform%2Fr-server%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3E%20https%3A%2F%2Fwww.microsoft.com%2Fen-gb%2Fcloud-platform%2Fr-server%20%3C%2FA%3E%20Microsoft%20R%20Server%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-379071%22%20slang%3D%22en-US%22%3EFirst%20published%20on%20MSDN%20on%20Jul%2025%2C%202017%20%26nbsp%3BGuest%20post%20by%20Slaviana%20Pavlovich%20Microsoft%20Student%20PartnerMy%20name%20is%20Slaviana%20Pavlovich.%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-379071%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EData%20Science%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Edata%20science%20dsvm%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Efaculty%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Elearning%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Emicrosoft%20student%20partner%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Er%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Er%20server%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Estudent%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Estudents%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Microsoft
First published on MSDN on Jul 25, 2017


Guest post by Slaviana Pavlovich Microsoft Student Partner



My name is Slaviana Pavlovich. I am an IT and Management student at University College London with a passion for data science. I recently completed the Microsoft Professional Program for Data Science, where I developed core skills to work with data. If you are also interested in this career, but not sure where to start - I strongly encourage you to check it out. I also have a wide range of interests including 3D bioprinting, public speaking, and politics. Additionally, I enjoy swimming and photography to balance out my studies. I became a Microsoft Student Partner at the end of my first year and I absolutely enjoy being part of such a vibrant community. If you have any questions, feel free to ask!

Introduction


In today’s article, I am going to continue talking about R. In the second part of this two-part introduction to R (the first part is available here ), we are going to consider:

• Matrices

• Data Frames

• Functions

• Conditionals

• Loops

Matrices


Matrix is another data type that we are going to look at. Matrix is a two-dimensional data set. A matrix is created using the function matrix() :


> # creating a matrix

> example <- matrix(c(99,45,4,47,2,5), nrow = 3, ncol = 2, byrow = TRUE)

> example

[,1] [,2]

[1,] 99 45

[2,] 4 47

[3,] 2 5




As you can see in the example above, nrow and ncol are used to define the values for rows and columns. Also, byrow = TRUE means that the matrix is filled by rows, while byrow=FALSE – by columns. Let’s look at the following example:


> # creating a 2x3 matrix that contains the numbers from 1 to 6 and filled by columns

> example.2 <- matrix(1:6, nrow = 2, ncol = 3, byrow = FALSE)

> example.2

[,1] [,2] [,3]

[1,] 1 3 5

[2,] 2 4 6




To change the names of rows and columns of the matrix use dimnames :


> # creating a matrix

> A <- matrix(1:6, nrow = 3, byrow = TRUE)

> # setting row and column names

> A

[,1] [,2]

[1,] 1 2

[2,] 3 4

[3,] 5 6

> dimnames(A) = list(c("1row", "2row", "3row"), c("1col", "2col"))

> A

1col 2col

1row 1 2

2row 3 4

3row 5 6




There are certain operations you can do with matrices. You can transpose a matrix, using a function t() :


> M <- matrix(c(14,2,4,3,2,5), nrow = 2, ncol = 3, byrow = TRUE)

> M

[,1] [,2] [,3]

[1,] 14 2 4

[2,] 3 2 5

> t(M)

[,1] [,2]

[1,] 14 3

[2,] 2 2

[3,] 4 5




Furthermore, use solve() function to find an inverse of a square matrix:


> X <- matrix(1:4, nrow = 2, byrow = TRUE)

> X

[,1] [,2]

[1,] 1 2

[2,] 3 4

> solve(X)

[,1] [,2]

[1,] -2.0 1.0

[2,] 1.5 -0.5




Arithmetic operations are done element-wise:


> A <- matrix(1:6, nrow = 3, byrow = TRUE)

> B <- matrix(1:6, nrow = 2, byrow = TRUE)

> A

[,1] [,2]

[1,] 1 2

[2,] 3 4

[3,] 5 6

> B

[,1] [,2] [,3]

[1,] 1 2 3

[2,] 4 5 6

> A + 2

[,1] [,2]

[1,] 3 4

[2,] 5 6

[3,] 7 8

> B / 2

[,1] [,2] [,3]

[1,] 0.5 1.0 1.5

[2,] 2.0 2.5 3.0




For matrix multiplication use “%*%”:


> A %*% B

[,1] [,2] [,3]

[1,] 9 12 15

[2,] 19 26 33

[3,] 29 40 51




In R, to select elements of the matrix, do the following:


> K <- matrix(4:7, nrow = 2, byrow = TRUE)

> K

[,1] [,2]

[1,] 4 5

[2,] 6 7

> K[1,2] # element at 1st row and 2rd column

[1] 5

> K[1,] # first row

[1] 4 5

> K[,2] # second column

[1] 5 7




Finally, we can always modify a matrix:


> V <- matrix(1:9, ncol = 3)

> V

[,1] [,2] [,3]

[1,] 1 4 7

[2,] 2 5 8

[3,] 3 6 9

> V[1,3] <- 0; V # modify a single element at 1st row and 3rd column to 0

[,1] [,2] [,3]

[1,] 1 4 0

[2,] 2 5 8

[3,] 3 6 9

> V[V>2] <- 1; V # change all elements greater than 2 to 1

[,1] [,2] [,3]

[1,] 1 1 0

[2,] 2 1 1

[3,] 1 1 1




Data Frames


After looking at matrices, I suggest learning about data frames. A data frame is a special case of a list (another data object in R that was previously considered in the first part of the article ). Data frames are used for storing tables. Unlike matrices, each column, also known as a vector, can store different types of data (logical, numeric, character, complex, etc.). The function data.frame() is used to create a data frame:


> # creating a data frame

> table <- data.frame(name=c("Jack", "Karan", "Thomas", "Vito", "Kristine"),

+ age=c(19, 20, 19, 19, 19),

+ sex=c("M", "M", "M", "M", "F"),

+ colour=c("yellow", "red", "green", "blue", "pink"))

> table

name age sex colour

1 Jack 19 M yellow

2 Karan 20 M red

3 Thomas 19 M green

4 Vito 19 M blue

5 Kristine 19 F pink

> typeof(table)

[1] "list"

> class(table)

[1] "data.frame"

> # function of a data frame

> names(table)

[1] "name" "age" "sex" "colour"

> nrow(table)

[1] 5

> ncol(table)

[1] 4




There are several ways of accessing an element of a data frame:


> table[2:4] # columns starting from 2nd to 4th of data frame

age sex colour

1 19 M yellow

2 20 M red

3 19 M green

4 19 M blue

5 19 F pink

> table[c("colour","age")] # columns with the titles colour and age from data frame

colour age

1 yellow 19

2 red 20

3 green 19

4 blue 19

5 pink 19




In a similar way to matrices, it is possible to change the values of the elements:


> table[3,"age"] <- 20; table # modify the element at 3st row and column age to 20

name age sex colour

1 Jack 19 M yellow

2 Karan 20 M red

3 Thomas 20 M green

4 Vito 19 M blue

5 Kristine 19 F pink




Functions


There is a straightforward way of creating own functions in R. Let’s consider an example where our function is going to find the difference between two integers:


> example <- function (a, b) {

+ c <- a - b

+ c

+ }

> example(15, 1)

[1] 14




As shown above, the word function is used to declare a function in R. Now we are going to create a function that prints a type and class of an argument:


example<-function(X){

+ print(typeof(X))

+ print(class(X))



> example <- function (a, b) {

+ c <- a - b

+ c

+ }

> example(15, 1)

[1] 14



+ print(paste("The type is", typeof(X) , "and class is", class(X)))

+ }

> Y<-c("Vito")

> example(Y)

[1] "character"

[1] "character"

[1] "The type is character and class is character"

> Z<-c(11)

> example(Z)

[1] "double"

[1] "numeric"

[1] "The type is double and class is numeric"




If you want to take an input from the user, use the function readline() in R:


read.example <- function()

+ {

+ str <- readline(prompt="Your name: ")

+ return(as.character(str))

+ }

> print(paste("Nice to meet you,", read.example(), "!"))

Your name: Dre

[1] "Nice to meet you, Dre !"




Conditionals


To use conditional execution in R, we are going to use if…else statement:


> x <- 4

> if (x < 0) {

+ print("It is a negative number!")

+ } else if (x > 0) {

+ print("It is a positive number!")

+ } else

+ print("Zero!")

[1] "It is a positive number!"



> x <- -10

> if (x < 0) {

+ print("It is a negative number!")

+ } else if (x > 0) {

+ print("It is a positive number!")

+ } else

+ print("Zero!")

[1] "It is a negative number!"



> x <- 0

> if (x < 0) {

+ print("It is a negative number!")

+ } else if (x > 0) {

+ print("It is a positive number!")

+ } else

+ print("Zero!")

[1] "Zero!




Loops

Now we are going to consider the control statements in R, such as for{}, repeat{} and while{}.

· A for{} loop in the example below is going to print the first three numbers in the vector Y:


> Y <- c(17, 25, 19, 33, 11, 51, 55)

> for(i in 1:3) {

+ print(Y[i])

+ }

[1] 17

[1] 25

[1] 19




· In this example, a repeat{} loop is going to print “task” and after 3 loops it is going to break:


> task <- c("R is great!")

> i <- 3

> repeat {

+ i <- i + 1

+ print(task)

+ if(i > 5) {

+ break

+ }

+ }

[1] "R is great!"

[1] "R is great!"

[1] "R is great!"




· A while{} loop is going to follow the commands as long as the condition is true:


> i <- 1

> while(i < 5) {

+ print(i)

+ i <- i + 1

+ }

[1] 1

[1] 2

[1] 3

[1] 4




Resources

There are so many interesting resources online that can help you further with R. I strongly recommend checking them out. In the following article, I am going to cover data visualisation, stay updated!

https://academy.microsoft.com/en-us/professional-program/ Microsoft professional programmes, Big Data, Data Science
https://imagine.microsoft.com/en-us/Catalog R Server Download for Students & Academics via Imagine Access
https://docs.microsoft.com/en-us/r-server/ R Server and R Documentation
https://www.microsoft.com/en-gb/cloud-platform/r-server Microsoft R Server