从一个 column/row 值等于列名的单元格中提取值
Extract value from cell where one column/row value is equal to the column name
我已经看到了几个关于此解决方案的主题,但我正在努力实施它们。我有一个 df,顶部的列带有描述,然后我有一个样本列表,其中包含按描述分组的数据。我需要提取描述与列名匹配的值。
我尝试了使用 match、cbind、sapply...等的不同解决方案,但收到有关无效类型(矩阵)或重复行名的错误。
df1
#row description sample ball square circle
1 ball 1a .78 .04 .22
2 ball 7b3 .32 .33 .33
3 square aaabc .02 .90 .05
4 circle ggg3 .05 .04 .90
5 circle 44 .01 .25 .70
我的输出是:
df2
#row description sample value
1 ball 1a .78
2 ball 7b3 .32
3 square aaabc .90
4 circle ggg3 .90
5 circle 44 .70
然后更进一步,我会过滤它
df2 %>%
filter(value < .9) %>%
select(description, sample, value)
导致:
#row description sample value
1 ball 1a .78
2 ball 7b3 .32
3 circle 44 .70
我知道这是重复的,我只是想知道为什么我无法获得处理此数据集的解决方案。
我们可以使用 row/column 索引来提取 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"))
看来你有可能性百分比。因此,您基本上是在尝试提取出现可能性最高的列,例如在这 3 行中提取每行的最大值。所以:
首先我们创建一个函数来提取 3 列中每行的最大值
funcionMax <- function(unDf) {
numFilas <- nrow(unDf)
vectorMax <- vector()
for(i in 1:numFilas)
{
vectorMax[i]<- max(unDf[i,1],unDf[i,2],unDf[i,3])
}
vectorMax
}
然后,我们子集只处理这 3 列,并应用新函数:
vectorFuncionMax <- df %>% select(ball,square,circle) %>% funcionMax
cbind(df,vectorFuncionMax)
就是这样。不客气。
我已经看到了几个关于此解决方案的主题,但我正在努力实施它们。我有一个 df,顶部的列带有描述,然后我有一个样本列表,其中包含按描述分组的数据。我需要提取描述与列名匹配的值。
我尝试了使用 match、cbind、sapply...等的不同解决方案,但收到有关无效类型(矩阵)或重复行名的错误。
df1
#row description sample ball square circle
1 ball 1a .78 .04 .22
2 ball 7b3 .32 .33 .33
3 square aaabc .02 .90 .05
4 circle ggg3 .05 .04 .90
5 circle 44 .01 .25 .70
我的输出是:
df2
#row description sample value
1 ball 1a .78
2 ball 7b3 .32
3 square aaabc .90
4 circle ggg3 .90
5 circle 44 .70
然后更进一步,我会过滤它
df2 %>%
filter(value < .9) %>%
select(description, sample, value)
导致:
#row description sample value
1 ball 1a .78
2 ball 7b3 .32
3 circle 44 .70
我知道这是重复的,我只是想知道为什么我无法获得处理此数据集的解决方案。
我们可以使用 row/column 索引来提取 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"))
看来你有可能性百分比。因此,您基本上是在尝试提取出现可能性最高的列,例如在这 3 行中提取每行的最大值。所以:
首先我们创建一个函数来提取 3 列中每行的最大值
funcionMax <- function(unDf) {
numFilas <- nrow(unDf)
vectorMax <- vector()
for(i in 1:numFilas)
{
vectorMax[i]<- max(unDf[i,1],unDf[i,2],unDf[i,3])
}
vectorMax
}
然后,我们子集只处理这 3 列,并应用新函数:
vectorFuncionMax <- df %>% select(ball,square,circle) %>% funcionMax
cbind(df,vectorFuncionMax)
就是这样。不客气。