Program R Flashcards
sqrt(36)=6
abs(-16)=16
?sqrt
this code will return the manual of sqrt
if some variable name is not built, we can create them. i.e. type “squareroot2=sqrt(2)” then enter, then next time, when we type in squareroot2, the R will return the value 1.414… here is we don’t use “=”, we can also use “
lower case and capital letter makes differences when we write the codes in R.
ls(), this will return a list of created variable names.
no space in variable names. i.e. Square Root is not correct.
don’t put number at the beginning of a variable name. i.e. 2SquareRoot is not correct.
but it’s allowed to put comma in the middle of a variable name i.e. Square2.Root is okay.
we can also create a victor, a list of numbers or characters. but cannot have them at the same time.
i.e. we can type in “Country = c(“Brazil”,”China”,”India”,”Switzerland”,”USA”)”, or “LifeExpectancy = c(74,76,65,83,79)”. then when we enter “Country” or “LifeExpectancy” then click on enter, the R will give us the list of country names and the numbers.
when there is victor, we type in the name “Country [1]” then enter, the R will give us the first country name in the list.
seq(0,100,2) will give us the numbers from 0 to 100 with increase by 2. i.e. 0, 2, 4, 6, ….100
we can also create a data frame.
type “CountryData = data.frame(Country,LifeExpectancy)”,
then in next line, type CountryData, the R will give us two columns of data with combined established country names and life expectancy ages. then if in next line, we type CountryData$Population=c(199000,1390000,1240000,7997,318000), then next line type CountryData, R will give us a data frame with 3 columns, with added population numbers.
variable “rbind” can combine lines with the same columns and headers of columns.
to combine two victors into a data frame, we use data.frame function. i.e. ….=data.frame(x,y)
to add a variable to the data frame, use dollar sign function. i.e. CountryData$Population=c(…,…)
to add a new observation to a data frame, use rbind function. i.e. ….=rbind(x,y)
WHO = read.csv(“WHO.csv”)
enter “str(WHO)”
R wil give us the data structure of the WHO file
Factor variables are categorical variables that can be either numeric or string
function “summary” can give us a summary of a data
use “write.csv” can save a data file in the writing directory based on instructions. i.e. write.csv(WHO_Europe,”WHO_Europe.csv”)
use “rm(…)” function to remove an created variable
in the summary, if there is “NA’s”, it means some data are missing
what is time format in R?
Month/day/year/ hour and minute
how to extract the month and dates in R?
here “mvt” is the name of the file. “DateConvert” is the data frame that contains the converted dates.
mvt$Month = months(DateConvert)
mvt$Weekday = weekdays(DateConvert)
how to create a table in R with one column of data?
table(mvt$Month)
there mvt is the name of the file, Month is the name of the column containing the data
how to create a histogram?
hist(mvt$Date, breaks=100)
mvt is the file, date is the data.