简体   繁体   English

用R中的字符串重塑数据帧

[英]reshaping dataframe with strings in R

I have this dataframe: 我有这个数据框:

v1<-c("a", "cat1", "c", rep(1:5) )
v2<-c("b", "cat2", "c1", rep(6:10) )
v3<-c("c", "cat3", "c3", rep(11:15) )
v4<-c("a", "cat1", "c", rep(16:20) )
v5<-c("b", "cat2", "c1", rep(21:25) )
v6<-c("c", "cat3", "c3", rep(26:30) )

date<- c("some.chart", NA, NA, 2010:2014)

data.frame(date, v1,v2,v3,  v4,v5,v6)

which gives the following messy data: 它提供了以下凌乱的数据:

 date   v1   v2   v3   v4   v5   v6
1 fgfh    a    b    c    a    b    c
2       <NA> cat1 cat2 cat3 cat1 cat2 cat3
3       <NA>    c   c1   c3    c   c1   c3
4       2010    1    6   11   16   21   26
5       2011    2    7   12   17   22   27
6       2012    3    8   13   18   23   28
7       2013    4    9   14   19   24   29
8       2014    5   10   15   20   25   30

You can see than my main problem is that is messy in its first rows. 您可以看到比我的主要问题还多的是第一行。 As an example of what I expect to see is this: 作为我期望看到的一个例子是:

countries category1 category2 category3 year x1 x2
1        v1         a      cat1         c 2010  1  2
2        v2         b      cat2        c1 2010  6  7
3        v3         c      cat3        c3 2010 11 12
4        v4         a      cat1         c 2011 16 17
5        v5         b      cat2        c1 2011 21 22
6        v6         c      cat3        c3 2011 26 27

Basically, I want to reshape it, but the obvious practical problem is the first rows which are strings. 基本上,我想重塑它,但是明显的实际问题是字符串的第一行。

Can someone help me with some ideas about what can I do ? 有人可以帮我一些有关我该怎么做的想法吗?

I suggest the following: 我建议以下内容:

df <- data.frame(date, v1,v2,v3,  v4,v5,v6)
df <- t(df)
colnames(df) <- df[1,]
df <- df[-1,]
colnames(df)[1] <- "cat2"
colnames(df)[2] <- "cat3"
df <- cbind(rownames(df), df)
colnames(df)[1] <- "cat1"
colnames(df)[4] <- "cat4"

library(tidyr)
gather(as.data.frame(df), key="year", value="x", "2010":"2014")

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

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