生信入门 第五天

2024-06-12 10:07:00 浏览数 (2)

This is my day 5 homework for BIC by 生信星球.

Today's content is the introduction of data structure.

Before study, gain the demo data from wechat public account.

tips:

(1) the assignment symbol for R is <-, which can also be replaced by =. This answers my question in previous study.

(2) console equals command line in Linux

(3) the code in R includes (), which must be English input

(4) getwd() shows the working directory

(5) vector is made up of element which can be number or string

(6) table is called data frame (df) in R

(7) read help document by ?read.table, try example

(8) data type, focus on vector and data frame

1. vector

1.1 vector is a list of items that are of the same type

the associations among variable, item, vector.

代码语言:R复制
x<- c(1,2,3) 
x<- 1:10  # from 1 to 10 , 10 integers 

1.2 extract items from vetor:

(1) by location

代码语言:R复制
x[4]  # the 4th item in x
x[-4] # reverse choose, exclude the 4th item in x and include others
x[2:4] #from 2nd to 4th 
x[-(2:4)] # reverse choose
x[c(1,5)] # the 1st and 5th

(2) by value

代码语言:R复制
x[x==10] # item equals 10 in x
x[x<0] # items less than 0 in x
x[x %in% c(1,2,5)] # items in vector c(1,2,5)

2. data frame

the demo data must be in the working directory

2.1 read local data

代码语言:R复制
read.table(file = "huahua.txt", sep = "t", header = T)
a <- read.table(file = "huahua.txt", sep = "t", header = T)

2.2 check line name, numbers of lines and colunms

代码语言:R复制
colnames(a)  #check the colunm name
rownames(a) # check the line name
dim(a) #check the structre of data frame

2.3 save data frame

代码语言:R复制
write.table(a, file = "yu.txt", sep = ",", quote = F)

2.4 save vairables and load variables

代码语言:R复制
save.image(file="bioinfoplanet.RData") #save current image
save(a,file="test.RData") # save a as test.RData
load("test.RData") # load test.RData

2.5 extract items

by $ symbol

by location of coordinate

代码语言:R复制
a[x,y] 

by name of colunm

代码语言:R复制
a[x,]
a[,y]
a[c(a,b)] #a colunm and b colunm

2,6 directly use the variables in data frame

代码语言:R复制
plot(iris$Sepal.length, iris$Sepal.Width)

save(a,file="test.RData") report: object a not found

check whether the object a is in the right-up section of Rstudio

0 人点赞