0

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.

neilfws
  • 32,751
  • 5
  • 50
  • 63
genbio
  • 3
  • 2
  • Welcome to Stack Overflow. We cannot read data into R from images. Please [make this question reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) by including a small representative dataset in a plain text format - for example the output from `dput(yourdata)`, if that is not too large, or `dput(head(yourdata))` for the first few rows. – neilfws Aug 09 '23 at 01:56
  • Thanks for your reply. I am trying to make a table, but the formatting is getting different after posting it. – genbio Aug 09 '23 at 04:31

1 Answers1

1

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

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")

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 <- c(1,2)
col_values <- c("D", "B")

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)
#>    id value column
#> 1 p_1    30      D
#> 2 P_2    27      B

Created on 2023-08-09 with reprex v2.0.2

DaveArmstrong
  • 18,377
  • 2
  • 13
  • 25