R language combat-reading notes (Chapter 4 graphics preliminary)

***********************************

Script file combined with map:
create script: file-create a new script program, copy and paste the following code into the script, select the right button to run the current or selected code.

# CHAPTER 4 BASIC DATA MANAGEMENT #

#创建数据集#
manager <- c(1, 2, 3, 4, 5)
date <- c("10/24/08", "10/28/08", "10/1/08", "10/12/08", "5/1/09")
country <- c("US", "US", "UK", "UK", "UK")
gender <- c("M", "F", "F", "M", "F")
age <- c(32, 45, 25, 39, 99)
q1 <- c(5, 3, 3, 3, 2)
q2 <- c(4, 5, 5, 3, 2)
q3 <- c(5, 2, 5, 4, 1)
q4 <- c(5, 5, 5, NA, 2)
q5 <- c(5, 5, 2, NA, 1)
leadership <- data.frame(manager, date, country, gender, age,
q1, q2, q3, q4, q5, stringsAsFactors=FALSE)

#创建新变量#
mydata<-data.frame(x1 = c(2, 2, 6, 4),
x2 = c(3, 4, 2, 8))
mydata$sumx <- mydata$x1 + mydata$x2
mydata$meanx <- (mydata$x1 + mydata$x2)/2

attach(mydata)
mydata$sumx <- x1 + x2
mydata$meanx <- (x1 + x2)/2
detach(mydata)

mydata <- transform(mydata,
sumx = x1 + x2,
meanx = (x1 + x2)/2)
mydata

#变量重编码#
leadership <- within(leadership,{
agecat <- NA
agecat[age > 75] <- "Elder"
agecat[age >= 55 & age <= 75] <- "Middle Aged"
agecat[age < 55] <- "Young" })
leadership

#变量重命名#
install.packages("plyr")
library(plyr)
leadership <- rename(leadership,
c(manager="managerID", date="testDate"))
leadership


#Missing value # is.na (leadership [, 6: 10]) #Recoding
some values ​​to be missing values ​​#
leadership $ age [leadership $ age == 99] <
-NA leadership $ age #removing
missing values ​​#
x <-c (1, 2, NA, 3)
z1 <-x [1] + x [2] + x [3] + x [4]
#Result is NA # z2 <-sum (x) #Result is NA , Help (sum) View the sum function to deal with missing values, and find that the default is na.rm = FALSE, so it is changed to TRUE #
z3 <-sum (x, na.rm = TRUE)
y <-na.omit (x) 
z4 <-sum (y)
z1
z2
z3
z4

#DateFormat 設定#
leadership
myformat <-"% m /% d /% y"
leadership $ date <-as.Date (leadership $ testDate, myformat)
leadership

Sys.Date () #Can return the date of the day #
date ()
#Return the current date and time # format (Sys.Date (), format = "% A")
format (Sys.Date (), format = "% a ")

today <- Sys.Date()
dob <- as.Date("1956-10-12")
difftime(today, dob, units="weeks")

#Type conversion #
a <-c (1,2,3)
is.numeric (a)
a <-as.character (a)
a

#排序#
attach(leadership)
sex_order <-leadership[order(gender),]
sex_order
sex_age_order <- leadership[order(gender, age),] #-age为降序#
sex_age_order
detach(leadership)

#Extract the data, no-is to delete the data #
leadership
newdata1 <-leadership [c (2,3),] # Extract the second and third lines #
newdata1
newdata2 <-leadership [, c (2,3)] # Extract Columns 2 and 3 #
newdata2 
newdata3 <-leadership [c (-2, -3),] # delete line 2, 3 #
newdata3 
newdata4 <-leadership [, c (-2, -3)] # delete line 2 , 3 列 #
newdata4 #Data
Screening #
attach (leadership)
newdata < -leadership  [gender == 'M' & age> 30,]
newdata
detach (leadership)

Published 9 original articles · won 19 · views 6688

Guess you like

Origin blog.csdn.net/qq_36509256/article/details/105688601