学习小组Day6笔记--王陈

tidyr包的使用

  1. Reshape Data
a <- data.frame(country = c("A","B","C"),"1999"= paste(c(0.7,37,212),"K"),"2000"= paste(c(2,80,213),"K"))
gather(a,X1999,X2000,key = "year", value = "cases")##gather括号里的分别是:数据框名,需合并的列名(两个),合并后的key列名,value列名。
gather(a,"year","cases",X1999,X2000)
gather(a,year,cases,-country)
  1. Handle Missing Values
X<-read.csv("doudou.txt")##read.csv的默认分隔符是",",导出时不会默认加引号,转换成txt也不会乱码。
drop_na(X,X2)##删除有空值的整行
fill(X,X2)##根据上一行的数值填充
replace_na(X,list(X2=2))##同一列的空值填上同一个数,括号里填数据框名,要填的列名=要填的值
  1. Expand Tables
    • complete
X<- read.csv("doudou.txt")
complete(X,nesting(X1), fill = list(X2=5))##把空值的位置补全
  • expand
pin2 <- data.frame(GeneId=rep("gene5",3),SampleName=paste("Sample",1:3,sep = ""),Expression = c(14,19,18))
expand(pin2,GeneId,SampleName,Expression)##列出每列值所有可能的组合
  1. Split Cells
    图1.png

    separate(table3,rate,into=c("cases","pop")##按列分割
    图2.png

    separate_rows(table3,rate)##按行分割
    图3.png

    unite(table5,century,year,col="year",sep="")##合并
    思维导图
    Day6.png

你可能感兴趣的:(学习小组Day6笔记--王陈)