R语言基础入门(3) select选择列的方法从基础到高级

Python025

R语言基础入门(3) select选择列的方法从基础到高级,第1张

如果有很多的列具有相似的结构,可以通过starts_with(),ends_with()或contains( )来进行选择

也可以这样写

如果希望此列为实际列,则可以使用该 rownames_to_column()函数,并指定新的列名称

建议你选择subset函数

subset函数,从某一个数据框中选择出符合某条件的数据或是相关的列

(1)单条件查询

>selectresult=subset(df1,name=="aa")

>selectresult

name age sex

1 aa 20 f

>df1

name age sex

1 aa 20 f

2 bb 29 m

3 cc 30 f

(2)指定显示列

>selectresult=subset(df1,name=="aa",select=c(age,sex))

>selectresult

age sex

1 20 f

(3)多条件查询

>selectresult=subset(df1,name=="aa" &sex=="f",select=c(age,sex))

>selectresult

age sex

1 20 f

>df1

name age sex

1 aa 20 f

2 bb 29 m

3 cc 30 f