当前位置:网站首页>List and data frame of R language experiment III

List and data frame of R language experiment III

2022-07-06 14:04:00 Baa Baa_ ten million five hundred and thirty-eight thousand sev

list

1、 establish

1.1 Create a list L1, Record the names of three students (A,B,C), Number (1,2,3), Math scores (80,90,95), Chinese achievement (75,95,80)

L1<-list(` full name `= c('A','B','C'),` Number `= c(1,2,3),` Math scores `=c(83,77,90),` Chinese achievement `=c(73,65,82))
L1

1.2 take L1 Component names are changed to English , Write it down as L2

L2 <- L1
names(L2) <- c('name','id','math','lit')
L2

 

1.3 take L1 and L2 As L3 Two components of

L3 <- list(l1 = L1,l2 = L2)
L3

2、 Indexes

2.1 Try indexing in different ways L1 Data in the

L1$` full name `[1]  # Check the first name 
L1[1]    # Look at the first component 
L1[[1]][2]   # Look at the second element of the first component 

 

 

2.2  A What is your math score

L1$` mathematics `[L1$` full name `=='A']

2.3 The math score is greater than 85 Who is the classmate of the score ?

L1$` full name `[L1$` Math scores `>85]

2.4 The average score exceeds 85 Who is the classmate of the score ?

L1$` full name `[(L1$` Math scores `+L1$` Chinese achievement `)/2>85]

3、 Characteristics and operation

3.1 list L1 There are several components in

length(L1)

3.2 How many numerical data are stored in the list

is.numeric(L1[[1]])+is.numeric(L1[[2]])+is.numeric(L1[[3]])+is.numeric(L1[[4]])

3.3 take L1 and L2 Combine them into a new list and record it as L3

L3 <- c(L1,L2)
L3

Data frame

1、 establish

1.1 Create a data frame df1, Record the names of three students (A,B,C), Number (1,2,3), Math scores (80,90,95), Chinese achievement (75,95,80)

df1 <- data.frame(name = c('A','B','C'),id = c(1,2,3), math = c(80,90,95), chinese= c(75,95,80))
df1

1.2 Set the row name of the data frame to name

rownames(df1) <- df1$name
df1

2、 Indexes

2.1 Try indexing in different ways df1 Data in the

df1[1]  # First column 
df1[[3]]  # The third column 
df1$chinese  #chinese Column 

 

2.2 A What is your math score

df1$math[df1$name=='A']

 

2.3 The math score is greater than 85 Who is the classmate of the score ?

df1$name[df1$math>85]

 

2.4 The average score exceeds 85 Who is the classmate of the score ?

df1$name[(df1$math+df1$chinese)/2>85]

 

3、 Characteristics and operation

3.1 Get the row name of the data frame

rownames(df1)

 

3.2 Get the number of rows and columns of the data frame

dms <- dim(df1)
dms

3.3 Create a data frame df3, Record the names of three students (A,B,C), English scores (85,80,95), take df3 Information and df1 The information of is merged into df5

df3 <- data.frame(name = c('A','B','C'),english= c(85,80,95))
df5 <- merge(df1,df3)  # Merge by columns 

 

3.4 Create a data frame df4, Record the name of a classmate (D), Number 4, Math scores 80, Chinese achievement 95, English scores 85, take df4 Information and df5 The information of is merged into df6

df4 <- data.frame(name = 'D', id = 4, math = 80, chinese = 95, english = 85)
df6 <- rbind(df5,df4)  # Merge by line 
df6 

原网站

版权声明
本文为[Baa Baa_ ten million five hundred and thirty-eight thousand sev]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202131425194684.html