使用 R 数据框的列名中的字符串提取列。

要在 R 数据框的列名中提取具有特定字符串的列,我们可以对列名使用 grepl 函数,然后使用单个方括号对数据框进行子集化。

例如,如果我们有一个名为 df 的数据框,并且我们想要提取名称中包含 X 的列,那么我们可以使用下面提到的命令 -

df[grepl("X",colnames(df))]

示例 1

以下代码段创建了一个示例数据框 -

Students_Score<-sample(1:50,20)

Teachers_Rank<-sample(1:5,20,replace=TRUE)

Teachers_Score<-sample(1:50,20)

df1<-data.frame(Students_Score,Teachers_Rank,Teachers_Score)

df1

创建了以下数据框

   Students_Score Teachers_Rank Teachers_Score

1              37             3             42

2              50             4             15

3               8             5             21

4              29             3             35

5              10             5              3

6               2             2             41

7              12             4             29

8               1             4             44

9              41             2             10

10             39             3             39

11             27             3             43

12             18             1             48

13             44             5             12

14             21             4             16

15             16             3             20

16             45             5             50

17             17             1             31

18             49             1             30

19             47             5             17

20             32             5              8

要提取在上面创建的数据框中的列名中包含 Score 的 df1 列,请将以下代码添加到上述代码段中 -

Students_Score<-sample(1:50,20)

Teachers_Rank<-sample(1:5,20,replace=TRUE)

Teachers_Score<-sample(1:50,20)

df1<-data.frame(Students_Score,Teachers_Rank,Teachers_Score)

df1[grepl("Score",colnames(df1))]

输出结果

如果您将上述所有给定的片段作为单个程序执行,它会生成以下输出 -

 Students_Score Teachers_Score

1            37             42

2            50             15

3             8             21

4            29             35

5            10              3

6             2             41

7            12             29

8             1             44

9            41             10

10           39             39

11           27             43

12           18             48

13           44             12

14           21             16

15           16             20

16           45             50

17           17             31

18           49             30

19           47             17

20           32              8

示例 2

以下代码段创建了一个示例数据框 -

Hot_Temp<-sample(33:50,20,replace=TRUE)

Cold_Temp<-sample(1:10,20,replace=TRUE)

Group<-sample(c("First","Second","Third"),20,replace=TRUE)

df2<-data.frame(Hot_Temp,Cold_Temp,Group)

df2

创建了以下数据框

  Hot_Temp Cold_Temp  Group

1       47         4  Third

2       33         5  First

3       36         2  Second

4       35         8  Second

5       33         8  First

6       44         1  Third

7       33         8  Third

8       46         3  First

9       36         3  Third

10      44         6  First

11      43        10  Third

12      35         9  First

13      36         4  Third

14      44         5  Second

15      48         5  Second

16      37         6  Second

17      35         5  Second

18      42         4  First

19      40         4  Second

20      42         4  Third

要提取上面创建的数据框中列名中包含 Temp 的 df2 列,请将以下代码添加到上述代码段中 -

Hot_Temp<-sample(33:50,20,replace=TRUE)

Cold_Temp<-sample(1:10,20,replace=TRUE)

Group<-sample(c("First","Second","Third"),20,replace=TRUE)

df2<-data.frame(Hot_Temp,Cold_Temp,Group)

df2[grepl("Temp",colnames(df2))]

输出结果

如果您将上述所有给定的片段作为单个程序执行,它会生成以下输出 -

 Hot_Temp Cold_Temp

1     47         4

2     33         5

3     36         2

4     35         8

5     33         8

6     44         1

7     33         8

8     46         3

9     36         3

10    44         6

11    43        10

12    35         9

13    36         4

14    44         5

15    48         5

16    37         6

17    35         5

18    42         4

19    40         4

20    42         4

以上是 使用 R 数据框的列名中的字符串提取列。 的全部内容, 来源链接: utcz.com/z/345690.html

回到顶部