This question already has answers here:
Transpose / reshape dataframe without “timevar” from long to wide format

(8个答案)


9个月前关闭。




我有一个看起来像这样的表:
r - 在R或python中重塑表的形状-LMLPHP
code to reproduce this dataframe in  R:
ID = c("0", "51", "7", "62", "1","10","5", "79", "62", "10","1","7")
mRNA = c("0", "0", "30", "1", "0", "14", "0", "1", "1", "16", "0", "0")
Centroid = c("d0","d0", "d0", "d0", "d1", "d1","d1", "d1", "d1", "d10", "d10", "d10")
df <- data.frame(ID,mRNA,Centroid)

我想重新格式化此数据,使其看起来像这样:

r - 在R或python中重塑表的形状-LMLPHP

到目前为止,我已经尝试过了:
r <- reshape(df, direction = "wide", idvar="Centroid", timevar="ID")

输出看起来接近我想要的东西,但还没有到那里(我不希望它使用ID重命名列)。

r - 在R或python中重塑表的形状-LMLPHP

我更喜欢R中的解决方案,但是如果您在python中有建议,我也可以尝试一下。
任何帮助将不胜感激!

最佳答案

我们可以为每个ID创建一个唯一的Centroid列,然后以宽格式对数据进行整形。

library(dplyr)

df %>%
  group_by(Centroid) %>%
  mutate(ID = paste0("mRNA_", row_number())) %>%
  tidyr::pivot_wider(names_from = ID, values_from = mRNA)

# Centroid mRNA_1 mRNA_2 mRNA_3 mRNA_4 mRNA_5
#  <fct>    <fct>  <fct>  <fct>  <fct>  <fct>
#1 d0       0      0      30     1      NA
#2 d1       0      14     0      1      1
#3 d10      16     0      0      NA     NA

关于r - 在R或python中重塑表的形状,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59538515/

10-12 17:22