将第一列的行转换为 R 中的列 [重复]

问题描述 投票:0回答:1

我想将第一列的行移动到列,将第二列的值移动到行

###### CREATE THE DATA FRAME ######

Hypothesis <- c("Hyp_1", "Hyp_2", "Hyp_3")
Estimates <- c(0.34, 0.01, 0.22)

df <- as.data.frame(cbind(Hypothesis, Estimates))

df

  Hypothesis Estimates
      Hyp_1      0.34
      Hyp_2      0.01
      Hyp_3      0.22

我想要的是下一个格式


Hyp_1 Hyp_2  Hyp_3
0.34   0.01   0.22

谢谢!!!!

我尝试了下一个代码但没有成功

dcast(df, Estimates~Hypothesis)

r dataframe row multiple-columns
1个回答
0
投票

试试这个:

library(tidyverse)
pivot_wider(df, names_from = Hypothesis, values_from = Estimates)

© www.soinside.com 2019 - 2024. All rights reserved.