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评论76阅读模式
英文:

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?

问题

你可以使用R语言编写代码来提取数据框中指定行和列的值。以下是一个示例代码:

# 创建一个示例数据框
df <- data.frame(
  id = c("P_1", "P_2", "P_3", "P_4", "P_5", "P_6", "P_10008"),
  A = c(23, 20, 17, 14, 11, 8, NA),
  B = c(25, 27, 24, 24, 24, 24, NA),
  C = c(27, 25, 25, 25, 25, 25, NA),
  D = c(30, 30, 32, 33, 35, 30, NA),
  E = c(31, 28, 25, 22, 19, 16, NA),
  F = c(33, 32, 31, 30, 29, 28, NA),
  G = c(35, 26, 26, 22, 23, 20, NA),
  H = c(37, 26, 32, 38, 44, 50, NA),
  Cl = c(5, 3, 2, 6, 5, 6, NA)
)

# 创建一个新的数据框来存储提取的值
new_df <- data.frame(
  id = df$id,
  value = NA
)

# 使用循环遍历每一行,提取指定列的值
for (i in 1:nrow(df)) {
  row_index <- i
  col_index <- df$Cl[i]
  new_df$value[i] <- df[row_index, col_index]
}

# 打印提取后的数据框
print(new_df)

这段代码首先创建了一个示例数据框 df,然后创建了一个新的数据框 new_df 来存储提取的值。接下来,使用循环遍历每一行,提取指定列的值,并将其存储在 new_df 中的相应行。最后,打印出提取后的数据框 new_df

请注意,示例数据框中的缺失值用 NA 表示。你可以根据实际情况修改代码以适应你的数据框。希望对你有帮助!

英文:

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.html
匿名

发表评论

匿名网友

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

确定