繁体   English   中英

将列中的唯一值分成同一 dataframe 中的单独列

[英]Separating unique values in a column into separate columns within the same dataframe

我的数据列出了 2010 年和 2011 年消费的水果总数。 但是目前,每年的总数都在数据框的底部。 相反,我希望在新列中列出每年的总数。

我当前的数据框如下所示:

水果 全部的
苹果 863 2010
香蕉 224 2010
橙子 455 2010
苹果 934 2011
香蕉 453 2011
橙子 534 2011

但是我希望它看起来像这样:

水果 2010 2011
苹果 863 934
香蕉 224 453
橙子 455 534

我正在尝试使用 R

使用reshape的基本 R 选项

reshape(
  df,
  direction = "wide",
  idvar = "Fruit",
  timevar = "Year"
)

   Fruit Total.2010 Total.2011
1  Apple        863        934
2 Banana        224        453
3 Orange        455        534

#数据

> dput(df)
structure(list(Fruit = c("Apple", "Banana", "Orange", "Apple", 
"Banana", "Orange"), Total = c(863L, 224L, 455L, 934L, 453L,
534L), Year = c(2010L, 2010L, 2010L, 2011L, 2011L, 2011L)), class = "data.frame", row.names = c(NA,
-6L))

或者可以使用来自base R xtabs xtabs

xtabs(Total ~ Fruit + Year, df)
#        Year
#Fruit    2010 2011
#  Apple   863  934
#  Banana  224  453
#  Orange  455  534

数据

df <- structure(list(Fruit = c("Apple", "Banana", "Orange", "Apple", 
"Banana", "Orange"), Total = c(863L, 224L, 455L, 934L, 453L,
534L), Year = c(2010L, 2010L, 2010L, 2011L, 2011L, 2011L)),
class = "data.frame", row.names = c(NA,
-6L))

您可能只想将长格式转换为宽格式,这可以通过多种方式完成。

一个例子是使用 tidyr

通过运行下面的代码,您可以从原始宽 dataframe 创建一个新的 DF_wide,应该在单独的列中计算每年的总数。

library(tidyr)

DF_wide <- spread(DF_long, Year, Fruit, Total)

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM