我有一个包含以下列的数据框:
**Columns Country Year Number_of_deaths**
**Data** US 2000 25
US 2001 30
UK 2000 30
UK 2001 21
我想将其转换为以下格式:
**Columns: Country 2000 2001 2002 2003 2004**
**Data** US 25 30 35 40 25
UK 30 21 21 23 45
有人可以在R中给我示例代码来执行此操作吗?任何套餐都没问题。我们将非常感谢您的帮助。
答案 0 :(得分:3)
使用它:
希望它有效
library(reshape2)
dcast(data,country~year,value.var="No_of_deaths")
<强>输出:强>
country 2000 2001
1 UK 30 21
2 US 25 30
由于
答案 1 :(得分:3)
要求的小插图:
library(tidyr)
# creating sample data
dt = data.frame(country = rep(LETTERS[1:2], each=2),
year = 2000:2003,
num = c(25,30,30,21))
dt %>% spread(year, num)
# country 2000 2001 2002 2003
# 1 A 25 30 NA NA
# 2 B NA NA 30 21
答案 2 :(得分:1)
以下是使用base R
res <- reshape(df, timevar="Year", idvar="Country", direction="wide")
colnames(res) <- gsub(".*\\.", "",colnames(res)) #if you need `colnames` as `year` alone. But, it is not good to have `numeric` column names.
res
# Country 2000 2001
#1 US 25 30
#3 UK 30 21
如果您使用$
,请务必使用backticks
res$`2000`
#[1] 25 30
df <- structure(list(Country = c("US", "US", "UK", "UK"), Year = c(2000L,
2001L, 2000L, 2001L), Number_of_deaths = c(25L, 30L, 30L, 21L
)), .Names = c("Country", "Year", "Number_of_deaths"), class = "data.frame", row.names = c(NA,
-4L))