How do I extract value of a cell with the row and column numbers from a dataframe in R and then create a separate dataframe with the extracted values?

huangapple go评论69阅读模式
英文:

How do I extract value of a cell with the row and column numbers from a dataframe in R and then create a separate dataframe with the extracted values?

问题

我的数据在一个类似这样的数据框中:

id A B C D E F G H Cl
P_1 23 25 27 30 31 33 35 37 5
P_2 20 27 25 30 28 32 26 26 3
P_3 17 24 25 32 25 31 26 32 2
P_4 14 24 25 33 22 30 22 38 6
P_5 11 24 25 35 19 29 23 44 5
P_6 8 24 25 30 16 28 20 50 6
- - - - - - - - -
- - - - - - - - -
- - - - - - - - -
P_10008 - - - - - - - - -

我有一个包含超过10,000行和14列的数据框(示例中使用了8列)。我索引了需要提取值的行和列号码。 "Cl" 列包含了我想要从每一行提取值的列号码(也是 id 列)。例如,从第一行,我想要从 "D" 列提取值 "30",并且还想保留 id 列(P_1)。从第二行,我想要提取 id 为 P_2 的行中的 "B" 列的值。

新的数据集将如下所示:

id value
P_1 30
P_2 27
P_3 17
P_4 22
P_5 35
P_6 16
- -
- -
P_10008 -

我应该如何以 R 中的一种方式编写代码,以允许我提取整个数据集的单元格值?任何帮助将不胜感激。

英文:

My data are in a dataframe like this:

id A B C D E F G H Cl
P_1 23 25 27 30 31 33 35 37 5
P_2 20 27 25 30 28 32 26 26 3
P_3 17 24 25 32 25 31 26 32 2
P_4 14 24 25 33 22 30 22 38 6
P_5 11 24 25 35 19 29 23 44 5
P_6 8 24 25 30 16 28 20 50 6
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
P_10008 - - - - - - - - -

I have a dataframe with more than 10,000 rows and 14 columns (example attached in text using 8 columns). I indexed the row and column number from which I need to extract the value. The "Cl" column includes the column number (also id column) from which I would like to extract that value for each row. For example, from the first row I would like to extract the value “30” from the “D” column and also would like to keep the id column (P_1). Column “B” from the 2nd row with id P_2.

New dataset would be like,

id value
P_1 30
P_2 27
P_3 17
P_4 22
P_5 35
P_6 16
- -
- -
P_10008 -

How do I write the code in a way in R that would allow me to extract the value of a cell with the row and column number for my whole dataset? Any help would be appreciated.

答案1

得分: 1

以下是翻译好的部分:

首先,可以按照以下方式操作。首先,创建数据:

dat <- structure(list(id = c("p_1", "P_2", "P_3", "P_4", "P_5", "P_6", 
"P_7", "P_8", "P_9", "P_10"), A = c(23, 25, 23, 27, 30, 27, 23, 
25, 27, 30), B = c(25, 27, 25, 27, 30, 23, 24, 26, 28, 30), C = c(27, 
30, 27, 23, 25, 27, 30, 23, 16, 9), D = c(30, 23, 30, 13, 16, 
19, 22, 25, 28, 31), E = c(23, 34, 23, 25, 27, 30, 23, 24, 26, 
28)), row.names = c(NA, -10L), class = "data.frame")

您可以定义要提取的行索引和列名称。下面我们指定获取第1行的“D”列和第2行的“B”列:

row_index <- c(1, 2)
col_values <- c("D", "B")

接下来,您需要创建一个新的数据框,其中“id”值仅来自“id”的“row_index”值。 “value”列来自指定的列。您甚至可以将它保存为“column”列,以表示数字来自哪一列:

data.frame(id = dat$id[row_index], 
           value = dat[cbind(row_index, match(col_values, names(dat)))], 
           column = col_values)
#>    id value column
#> 1 p_1    30      D
#> 2 P_2    27      B

创建于2023-08-09,使用 reprex v2.0.2

英文:

You could do it as follows. First, make the data:

dat &lt;- structure(list(id = c(&quot;p_1&quot;, &quot;P_2&quot;, &quot;P_3&quot;, &quot;P_4&quot;, &quot;P_5&quot;, &quot;P_6&quot;, 
&quot;P_7&quot;, &quot;P_8&quot;, &quot;P_9&quot;, &quot;P_10&quot;), A = c(23, 25, 23, 27, 30, 27, 23, 
25, 27, 30), B = c(25, 27, 25, 27, 30, 23, 24, 26, 28, 30), C = c(27, 
30, 27, 23, 25, 27, 30, 23, 16, 9), D = c(30, 23, 30, 13, 16, 
19, 22, 25, 28, 31), E = c(23, 34, 23, 25, 27, 30, 23, 24, 26, 
28)), row.names = c(NA, -10L), class = &quot;data.frame&quot;)

You can define the row index and column names you want to extract. Below we are saying that take column "D" for row 1 and column "B" for row 2.

row_index &lt;- c(1,2)
col_values &lt;- c(&quot;D&quot;, &quot;B&quot;)

Next, you need to make a new data frame where the id value just comes from the row_index values of id. The value column comes from the indicated column. You could even have it save the column from which the number came as column:

data.frame(id = dat$id[row_index], 
           value = dat[cbind(row_index, match(col_values, names(dat)))], 
           column = col_values)
#&gt;    id value column
#&gt; 1 p_1    30      D
#&gt; 2 P_2    27      B

<sup>Created on 2023-08-09 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年8月9日 09:23:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76864002-2.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定