如何在R代码中查找值的行号 [英] How to find row number of a value in R code

查看:51
本文介绍了如何在R代码中查找值的行号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的一组值中,如何在第 4 列中找到特定值的行号??

例如,如何使用 R 代码命令找到第 4 列中值1578"的行号.

<代码>>我的数据_2性别 年龄 height_seca1 height_chad1 height_DL weight_alog11 F 19 1800 1797 180 70.02 楼 19 1682 1670 167 69.03 楼 21 1765 1765 178 80.04 楼 21 1829 1833 181 74.05 F 21 1706 1705 170 103.06 F 18 1607 1606 160 76.07 F 19 1578 1576 156 50.08 F 19 1577 1575 156 61.09 楼 21 1666 1665 166 52.010 F 17 1710 1716 172 65.011 F 28 1616 1619 161 65.512 F 22 1648 1644 165 57.513 F 19 1569 1570 155 55.014 F 19 1779 1777 177 55.015 米 18 1773 1772 179 70.016 米 18 1816 1809 181 81.017 米 19 1766 1765 178 77.018 米 19 1745 1741 174 76.019 米 18 1716 1714 170 71.020 万 21 1785 1783 179 64.021 万 19 1850 1854 185 71.022 米 31 1875 1880 188 95.023 米 26 1877 1877 186 105.524 米 19 1836 1837 185 100.025 米 18 1825 1823 182 85.026 米 19 1755 1754 174 79.027 米 26 1658 1658 165 69.028 万 20 1816 1818 183 84.029 米 18 1755 1755 175 67.0

解决方案

我很想使用 grepl,它应该给出所有匹配的行,并且可以推广到任意字符串.

>

mydata_2 <- read.table(textConnection("性别 年龄 height_seca1 height_chad1 height_DL weight_alog11 F 19 1800 1797 180 70.02 楼 19 1682 1670 167 69.03 楼 21 1765 1765 178 80.04 楼 21 1829 1833 181 74.05 F 21 1706 1705 170 103.06 F 18 1607 1606 160 76.07 F 19 1578 1576 156 50.08 F 19 1577 1575 156 61.09 楼 21 1666 1665 166 52.010 F 17 1710 1716 172 65.011 F 28 1616 1619 161 65.512 F 22 1648 1644 165 57.513 F 19 1569 1570 155 55.014 F 19 1779 1777 177 55.015 米 18 1773 1772 179 70.016 米 18 1816 1809 181 81.017 米 19 1766 1765 178 77.018 米 19 1745 1741 174 76.019 米 18 1716 1714 170 71.020 万 21 1785 1783 179 64.021 万 19 1850 1854 185 71.022 米 31 1875 1880 188 95.023 米 26 1877 1877 186 105.524 米 19 1836 1837 185 100.025 米 18 1825 1823 182 85.026 米 19 1755 1754 174 79.027 米 26 1658 1658 165 69.028 万 20 1816 1818 183 84.029 米 18 1755 1755 175 67.0"),sep = " ", 标题 = TRUE)其中(grepl(1578,mydata_2$height_seca1))

输出为:

<代码>>其中(grepl(1578,mydata_2$height_seca1))[1] 7>

然而,正如评论中指出的那样,这将捕获比字符串 1578 多得多的内容(例如,它也匹配 21578 等),因此只有在您确定值的长度时才应该使用您搜索的内容不会超过此处显示的四个字符或数字.

并且根据另一个答案进行子集设置也可以正常工作:

mydata_2[mydata_2$height_seca1 == 1578, ]性别 年龄 height_seca1 height_chad1 height_DL weight_alog17 F 19 1578 1576 156 50>

如果您正在寻找几个不同的值,您可以将它们放在一个向量中,然后使用 %in% 运算符:

look.for <- c(1578, 1658, 1616)>mydata_2[mydata_2$height_seca1 %in% look.for, ]性别 年龄 height_seca1 height_chad1 height_DL weight_alog17 F 19 1578 1576 156 50.011 F 28 1616 1619 161 65.527 米 26 1658 1658 165 69.0>

In the following set of values how can I find the row number of a particular value in column 4 ??

For example how can I find the row number of the value "1578" that's in column number 4 using the R code commands.

> mydata_2
   sex age height_seca1 height_chad1 height_DL weight_alog1
1    F  19         1800         1797       180         70.0
2    F  19         1682         1670       167         69.0
3    F  21         1765         1765       178         80.0
4    F  21         1829         1833       181         74.0
5    F  21         1706         1705       170        103.0
6    F  18         1607         1606       160         76.0
7    F  19         1578         1576       156         50.0
8    F  19         1577         1575       156         61.0
9    F  21         1666         1665       166         52.0
10   F  17         1710         1716       172         65.0
11   F  28         1616         1619       161         65.5
12   F  22         1648         1644       165         57.5
13   F  19         1569         1570       155         55.0
14   F  19         1779         1777       177         55.0
15   M  18         1773         1772       179         70.0
16   M  18         1816         1809       181         81.0
17   M  19         1766         1765       178         77.0
18   M  19         1745         1741       174         76.0
19   M  18         1716         1714       170         71.0
20   M  21         1785         1783       179         64.0
21   M  19         1850         1854       185         71.0
22   M  31         1875         1880       188         95.0
23   M  26         1877         1877       186        105.5
24   M  19         1836         1837       185        100.0
25   M  18         1825         1823       182         85.0
26   M  19         1755         1754       174         79.0
27   M  26         1658         1658       165         69.0
28   M  20         1816         1818       183         84.0
29   M  18         1755         1755       175         67.0

解决方案

I would be tempted to use grepl, which should give all the lines with matches and can be generalised for arbitrary strings.

mydata_2 <- read.table(textConnection("
sex age height_seca1 height_chad1 height_DL weight_alog1
1 F 19 1800 1797 180 70.0
2 F 19 1682 1670 167 69.0
3 F 21 1765 1765 178 80.0
4 F 21 1829 1833 181 74.0
5 F 21 1706 1705 170 103.0
6 F 18 1607 1606 160 76.0
7 F 19 1578 1576 156 50.0
8 F 19 1577 1575 156 61.0
9 F 21 1666 1665 166 52.0
10 F 17 1710 1716 172 65.0
11 F 28 1616 1619 161 65.5
12 F 22 1648 1644 165 57.5
13 F 19 1569 1570 155 55.0
14 F 19 1779 1777 177 55.0
15 M 18 1773 1772 179 70.0
16 M 18 1816 1809 181 81.0
17 M 19 1766 1765 178 77.0
18 M 19 1745 1741 174 76.0
19 M 18 1716 1714 170 71.0
20 M 21 1785 1783 179 64.0
21 M 19 1850 1854 185 71.0
22 M 31 1875 1880 188 95.0
23 M 26 1877 1877 186 105.5
24 M 19 1836 1837 185 100.0
25 M 18 1825 1823 182 85.0
26 M 19 1755 1754 174 79.0
27 M 26 1658 1658 165 69.0
28 M 20 1816 1818 183 84.0
29 M 18 1755 1755 175 67.0"),
                       sep = " ", header = TRUE)

which(grepl(1578, mydata_2$height_seca1))

The output is:

> which(grepl(1578, mydata_2$height_seca1))
[1] 7
> 

[Edit] However, as pointed out in the comments, this will capture much more than the string 1578 (e.g. it also matches for 21578 etc) and thus should be used only if you are certain that you the length of the values you are searching will not be larger than the four characters or digits shown here.

And subsetting as per the other answer also works fine:

mydata_2[mydata_2$height_seca1 == 1578, ]
  sex age height_seca1 height_chad1 height_DL weight_alog1
7   F  19         1578         1576       156           50
> 

If you're looking for several different values, you could put them in a vector and then use the %in% operator:

look.for <- c(1578, 1658, 1616)
> mydata_2[mydata_2$height_seca1 %in% look.for, ]
   sex age height_seca1 height_chad1 height_DL weight_alog1
7    F  19         1578         1576       156         50.0
11   F  28         1616         1619       161         65.5
27   M  26         1658         1658       165         69.0
> 

这篇关于如何在R代码中查找值的行号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆