如何在R数据帧中找到字符串的位置?
为了找到R数据帧中数值的位置,我们使用哪个函数,如果该值是字符串,则将使用相同的函数,但是我们需要适当地传递该值。例如,如果我们有一个名为df的数据框,其中包含一个值,例如tutor,那么我们可以使用命令which(df ==“ tutor”,arr.ind = TRUE)找到教师的位置。
例1
考虑以下数据帧-
> x1<-sample(c("2015","2018","2020"),20,replace=TRUE)输出结果> x2<-sample(c("2015","2018","2020"),20,replace=TRUE)
> x3<-sample(c("2015","2018","2020"),20,replace=TRUE)
> df1<-data.frame(x1,x2,x3)
> df1
x1 x2 x31 2018 2020 2018
2 2020 2020 2015
3 2018 2020 2015
4 2018 2015 2020
5 2018 2015 2018
6 2018 2020 2015
7 2015 2018 2020
8 2020 2018 2020
9 2015 2015 2018
10 2015 2015 2020
11 2020 2020 2020
12 2018 2018 2015
13 2015 2018 2015
14 2015 2018 2018
15 2015 2020 2020
16 2015 2020 2015
17 2015 2015 2018
18 2020 2015 2015
19 2020 2015 2015
20 2015 2020 2020
在df1中找到2020的位置-
> which(df1=="2020",arr.ind=TRUE)输出结果
row col[1,] 2 1
[2,] 8 1
[3,] 11 1
[4,] 18 1
[5,] 19 1
[6,] 1 2
[7,] 2 2
[8,] 3 2
[9,] 6 2
[10,] 11 2
[11,] 15 2
[12,] 16 2
[13,] 20 2
[14,] 4 3
[15,] 7 3
[16,] 8 3
[17,] 10 3
[18,] 11 3
[19,] 15 3
[20,] 20 3
例2
> y1<-sample(c("Asia","Africa","Europe"),20,replace=TRUE)输出结果> y2<-sample(c("Asia","Africa","Europe"),20,replace=TRUE)
> df2<-data.frame(y1,y2)
> df2
y1 y21 Europe Asia
2 Europe Africa
3 Africa Asia
4 Africa Africa
5 Europe Europe
6 Asia Asia
7 Europe Europe
8 Africa Europe
9 Africa Asia
10 Europe Africa
11 Europe Africa
12 Africa Europe
13 Europe Africa
14 Europe Asia
15 Africa Europe
16 Africa Asia
17 Europe Africa
18 Europe Africa
19 Asia Asia
20 Africa Asia
在df2中找到欧洲的位置-
> which(df2=="Europe",arr.ind=TRUE)输出结果
row col[1,] 1 1
[2,] 2 1
[3,] 5 1
[4,] 7 1
[5,] 10 1
[6,] 11 1
[7,] 13 1
[8,] 14 1
[9,] 17 1
[10,] 18 1
[11,] 5 2
[12,] 7 2
[13,] 8 2
[14,] 12 2
[15,] 15 2
以上是 如何在R数据帧中找到字符串的位置? 的全部内容, 来源链接: utcz.com/z/346666.html