Мы можем использовать индексирование строки / столбца для извлечения значений, которые match
имена столбцов со значениями столбца 'description'
m1 <- cbind(seq_len(nrow(df1)), match(df1$description, names(df1)[3:5]))
data.frame(df1[1:3], value = df1[3:5][m1])
# description sample ball value
#1 ball 1a 0.78 0.78
#2 ball 7b3 0.32 0.32
#3 square aaabc 0.02 0.90
#4 circle ggg3 0.05 0.90
#5 circle 44 0.01 0.70
или с tidyverse
library(tidyverse)
df1 %>%
rowwise %>%
transmute(description, sample, value = get(description))
# A tibble: 5 x 3
# description sample value
# <chr> <chr> <dbl>
#1 ball 1a 0.78
#2 ball 7b3 0.32
#3 square aaabc 0.9
#4 circle ggg3 0.9
#5 circle 44 0.7
данные
df1 <- structure(list(description = c("ball", "ball", "square", "circle",
"circle"), sample = c("1a", "7b3", "aaabc", "ggg3", "44"), ball = c(0.78,
0.32, 0.02, 0.05, 0.01), square = c(0.04, 0.33, 0.9, 0.04, 0.25
), circle = c(0.22, 0.33, 0.05, 0.9, 0.7)), class = "data.frame",
row.names = c("1",
"2", "3", "4", "5"))