分别从两个数据框中选择出条件相等的数据,组成一个新的数据框;
1.函数名:merge
> t1<-data.frame(name=c("a","b","c"),age=c(1,2,3),sex=c("m","m","f"));t1
name age sex 1 a 1 m 2 b 2 m 3 c 3 f
> t2<-data.frame(name=c("d","b","c"),age=c(4,5,6),sex=c("m","m","f"));t2 name age sex 1 d 4 m 2 b 5 m 3 c 6 f > m<-merge(t1,t2,by="name");m name age.x sex.x age.y sex.y 1 b 2 m 5 m 2 c 3 f 6 f
2.从某一个数据框中选择出某些条件的数据或是相关的列
函数名:subset()
#选出名字为"a"的相关信息
> test<-subset(t1,name=="a");test name age sex 1 a 1 m
>#选出名字为"a",的年龄和性别
test<-subset(t1,name=="a",select=c(age,sex));test age sex 1 1 m
> test<-subset(t1,name=="a"&sex=="m",select=c(age,sex));test age sex 1 1 m
> test<-subset(t1,name=="a"&sex=="f",select=c(age,sex));test [1] age sex <0 行> (或0-长度的row.names)>levels(mpg$manufacturer) [1] "audi" "chevrolet" "dodge" "ford" [5] "honda" "hyundai" "jeep" "land rover" [9] "lincoln" "mercury" "nissan" "pontiac" [13] "subaru" "toyota" "volkswagen"