简体   繁体   English

R:根据数据框中的值,用行名替换列名

[英]R: Replace Column Name with Row Name on Bases of a Value in Data Frame

I have one data frame as follows: 我有一个数据框,如下所示:

df <- data.frame(halloo = c(0.04,1,1,1), ciaoo = c(1,0.05,1, 1), bird=c(1,1,1,1))
row.names(df) <- c("hallo", "ciao", "else", "some")

Here I want to check if a value in a cell ist lower/equal 0.05 and if this is the case then I´d like to change the column name by the row name of the concerning cell. 在这里,我想检查单元格中的值是否小于或等于0.05,如果是这种情况,那么我想通过相关单元格的行名更改列名。

After applying a function, the final data frame should look like this: 应用函数后,最终数据帧应如下所示:

df.final <- data.frame(hallo = c(0.04,1,1,1), ciao = c(1,0.05,1, 1), bird=c(1,1,1,1))
row.names(df.final) <- c("hallo", "ciao", "else", "some")

Actually, although I tried to find one solution I have no idea for good one. 实际上,尽管我试图找到一种解决方案,但我不知道好的解决方案。 Has anyone an idea? 有人知道吗?

Thx in advance 提前Thx

We can use which with arr.ind=TRUE to get the row/column index matrix from the logical matrix ( df <= 0.05 ). 我们可以将whicharr.ind=TRUE一起使用which以从逻辑矩阵( df <= 0.05 )获得row/column索引矩阵。 Subset the column vector ('i2'), remove the duplicates ('i3' - as there could be more than one row in a column that meets the condition), assign the column names of 'df' based on 'i3' to the names of 'i3'. 子集列向量('i2'),删除重复项('i3'-因为满足条件的列中可能有多于一行),将基于'i3'的列名'df'分配给“ i3”的names

 i1 <- which(df <= 0.05, arr.ind=TRUE)
 i2 <-  i1[, 2]
 i3 <- i2[!duplicated(i2)]
 names(df)[i3] <- names(i3)

I rely heavily on subscripting and functionals for a lot of problems, so here's an alternate in that vein: 我在很多问题上都严重依赖下标和功能,因此这是一种替代方法:

bool <- df <= 0.05
bool <- as.data.frame(bool)
x <- pryr::compose(unlist, lapply)(
    bool,
    function(x) row.names(bool[x, TRUE][1])
    # [1] to handle duplicates - just pull row name of first match
)
names(df)[1:length(x)] <- x

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 R:将数据框x中的行名与数据框y中的列名匹配,以使两个数据框共享的列中的给定变量值 - R: match row name in data frame x with column name in data frame y for a given variable value in column shared by the two data frames 用另一个数据框中的colmn值合并/替换行名 - Merge/replace row name with colmn value from another data frame 有什么方法可以从 R 和 append 中的数据框列中拆分键和值作为数据框每一行的列名称和值? - Any ways to split key and value from a column of a data frame in R, and append the keys as columns name and value for each row of the data frame? 根据R中的条件获取data.frame的行列名 - Get row and column name of data.frame according to condition in R 提取行名的一部分以在 R 的数据框中创建一个新列 - Extracting parts of a row name to make a new column in a data frame in R 为 R 中的大型数据框逐行查找和替换条件列值 - Find and replace conditional column value by row for a large data frame in R 重塑R中的数据框:将列更改为行名 - reshaping data frame in R: changing a column to row name 如何用未知的列名替换某些数据框值? - How to replace certain data frame value with it's unknown column name? R数据框行名称显示矩阵而不是数值 - R data frame row name shows matrix instead of numeric value 替换列名和行名相同的数据帧单元格值 - Replacing data frame cell value where column and row name is the same
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM