需要重新组织数据

时间:2015-03-23 23:31:56

标签: r formatting data-management

我是R的新手并且有一个数据格式问题。 我需要改变这个:

Poly Tran Strat Surv MALLP MALLS MALLG MALLF GADWP GADWS GADWG GADWF
AL    1    M      y     1    2    0      0     1     4     0     0
ARL   1    M      y     0    0    0      0     0     0     20    0
AM    1    M      y     0    0    0      0     0     0     0     0
AM    2    M      y     1    0    0      0     0     0     0     5

到此:

Poly Tran Strat Surv  Spp   Num   Status
AL    1    M      y   mall   1      p
AL    1    M      y   mall   2      s
AL    1    M      y   gadw   1      p
AL    1    M      y   gadw   4      s
ARL   1    M      y   gadw   20     g
AM    2    M      y   mall   1      p
AM    2    M      y   gadw   5      f

我需要一些帮助! 三江源。

4 个答案:

答案 0 :(得分:3)

使用dplyrtidyr即可:

library(tidyr)
library(dplyr)

df %>% gather(Spp, Num, -Poly, -Tran, -Strat, -Surv) %>%
  mutate(Status = tolower(substr(Spp, 5, 5)),
         Spp = tolower(substr(Spp, 1, 4))) %>%
  filter(!Num == 0) %>%
  arrange(Tran)

给出了:

#  Poly Tran Strat Surv  Spp Num Status
#1   AL    1     M    y mall   1      p
#2   AL    1     M    y mall   2      s
#3   AL    1     M    y gadw   1      p
#4   AL    1     M    y gadw   4      s
#5  ARL    1     M    y gadw  20      g
#6   AM    2     M    y mall   1      p
#7   AM    2     M    y gadw   5      f

答案 1 :(得分:2)

使用基础R的解决方案(reshape2::melt除外):

dat2 <- reshape2::melt(dat, id.vars=c("Poly","Tran","Strat","Surv"))
dat2 <- subset(dat2, subset = value > 0)
dat2$variable <- as.character(dat2$variable)
dat2$Status <- with(dat2, substr(tolower(variable), nchar(variable), nchar(variable)))
dat2$variable <- with(dat2, substr(tolower(variable), 1, nchar(variable)-1))
dat2 <- dat2[order(dat2$Tran),]
colnames(dat2) <- c("Poly", "Tran", "Strat", "Surv", "Spp", "Num", "Status")
rownames(dat2) <- NULL

<强>结果

> dat2

  Poly Tran Strat Surv  Spp Num Status
1   AL    1     M    y mall   1      p
2   AL    1     M    y mall   2      s
3   AL    1     M    y gadw   1      p
4   AL    1     M    y gadw   4      s
5  ARL    1     M    y gadw  20      g
6   AM    2     M    y mall   1      p
7   AM    2     M    y gadw   5      f

数据

dat <- read.csv(text = "Poly,Tran,Strat,Surv,MALLP,MALLS,MALLG,MALLF,GADWP,GADWS,GADWG,GADWF
AL,1,M,y,1,2,0,0,1,4,0,0
ARL,1,M,y,0,0,0,0,0,0,20,0
AM,1,M,y,0,0,0,0,0,0,0,0
AM,2,M,y,1,0,0,0,0,0,0,5")

答案 2 :(得分:2)

为了好玩,这里有一个基础R方法。我已经取代了&#34; 0&#34;使用NA,以便na.omit可用于&#34;收缩&#34;在stack值之后关闭数据集。从那里开始,有一些基本的gsub bing来获得&#34; spp&#34;和&#34;状态&#34;值。

within(na.omit(
  cbind(dat[1:4], 
        stack(replace(dat[-c(1:4)], dat[-c(1:4)] == 0, NA)))), {
          ind <- tolower(ind)
          spp <- gsub("(mall|gadw).*", "\\1", ind)
          status <- gsub("mall|gadw", "", ind)
          rm(ind)
        })
#    Poly Tran Strat Surv values status  spp
# 1    AL    1     M    y      1      p mall
# 4    AM    2     M    y      1      p mall
# 5    AL    1     M    y      2      s mall
# 17   AL    1     M    y      1      p gadw
# 21   AL    1     M    y      4      s gadw
# 26  ARL    1     M    y     20      g gadw
# 32   AM    2     M    y      5      f gadw

正如多米尼克和史蒂文的答案所证明的那样,订购非常简单。

答案 3 :(得分:0)

这是使用data.table

dt.m = melt(dt, id=1:4, variable.name="Spp", value.name="Num")[Num != 0L]
dt.m[, c("Spp", "Status") := list(substring(Spp, 1, 4), substring(Spp, 5, 5))]

或者使用read.fwf

dt.m = melt(dt, id=1:4, variable.name="Spp", value.name="Num", variable.factor = FALSE)
dt.m[Num != 0L][, c("Spp", "Status") := read.fwf(textConnection(Spp), widths=c(4,1))]