[英]How change Row values to Column names (R)
I am working with COVID19 data.我正在处理 COVID19 数据。 There is one inconvenience.有一个不便之处。 I have a column named location and all countries are under that column.我有一个名为 location 的列,所有国家都在该列下。 To illustrate, first value is Country A, Date A, next Country A, Date B... Country Z, Date Z. I wonder how can I group all the values by dates and have each country as a separate column?为了说明,第一个值是国家 A,日期 A,下一个国家 A,日期 B...国家 Z,日期 Z。我想知道如何按日期对所有值进行分组并将每个国家作为单独的列?
This is the link of the data:这是数据的链接:
https://ourworldindata.org/coronavirus-source-data https://ourworldindata.org/coronavirus-source-data
You can reshape the data using code like the following:您可以使用如下代码重塑数据:
df_example = with(df, data.frame(location = location,
date = date,
new_cases = new_cases))
df_example = reshape(df_example, timevar = "location", idvar = "date", direction = "wide")
df_example = df_example[order(df_example$date), ]
Note here that I kept only one variable as the cell value in the new data frame (ie, new_cases), because the new data frame is already very wide (it has 213 columns now).请注意,我只保留了一个变量作为新数据框中的单元格值(即 new_cases),因为新数据框已经很宽了(现在它有 213 列)。 If you keep additional variables the new data frame will be wider.如果您保留其他变量,则新数据框将更宽。
head(df_example) # I will not put the output here, you can try yourself
The following three lines will make the new data frame look nicer.以下三行将使新的数据框看起来更好。
cnames = names(df_example)[2:ncol(df_example)]
cnames = unlist(lapply(cnames, function(x) substr(x, 11, nchar(x))))
names(df_example)[2:ncol(df_example)] = cnames
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.