【问题标题】:Create new dataframe with multiple columns based on single character column in R基于R中的单个字符列创建具有多列的新数据框
【发布时间】:2015-05-12 18:54:30
【问题描述】:

我有一份植物种类及其所在县的清单。我想创建一个新的数据框,其中包含植物种类和每个县的一列,如果植物出现在该县,则为 1,如果不在该县,则为 0。

以下是一些示例数据:

Accepted.Symbol County
ABRON   TX(Andrews, Armstrong, Bailey, Brewster)
ABAM2   TX(Brooks, Hidalgo, Jim Hogg, Kenedy, Kleberg, Live Oak, Starr)
ABAN    TX(Brewster, Culberson, El Paso, Ellis, Hudspeth, Presidio, Reeves)
ABCA    TX(Culberson)
ABFR2   TX(Andrews, Armstrong, Bailey, Briscoe)
ABMA5   TX(Freestone, Leon, Robertson)
ABUTI   TX(Andrews, Aransas, Atascosa, Bastrop)

县列表数据示例:

 Anderson
 Andrews
 Angelina
 Aransas
 Archer

这是我希望输出的样子(请注意,植物列的名称无关紧要,但县列的名称重要):

Plant  Anderson  Andrews
ABRON  0         1
ABAM2  0         0

我已经编写了一个函数来尝试这种重组,因为我必须定期更新它。在下面的函数中,“data”是带有县的工厂列表,“list”是所有县的单独列表。

county.list<-function(data, list) {
  output <- data.frame(data$Accepted.Symbol) #creates output dataset
    for (i in 1:length(list)) {
      county<-list[i]
    test<-grepl(as.character(county), data$County) #outputs T/F for county name
    test.1<-test*1                                 #converts T/F to 1/0
    output<-cbind(output, test.1)                #adds column to output dataset
    names(output)[names(output)=="test.1"] <- as.character(county) #renames column
    }
return(output)}

t1<-county.list(plants,counties)

当我运行这个函数时,我得到一个包含 2 列的数据框。第一个具有所有工厂代码。第二列全为 0,列名为“c(1,2,3,...,267)”。当我测试“for”循环之外的步骤(针对单个县)时,每一步都有效,所以我怀疑问题出在循环中。

我搜索了其他类似的问题,但没有一个能完全捕捉到我想要做的事情。如果效果更好,我愿意使用循环以外的方法。

提前致谢。

【问题讨论】:

  • 您想要每个县的指标吗?或者只是安德森和安德鲁斯
  • 您能否根据示例显示预期的输出,因为这很令人困惑

标签: r


【解决方案1】:

我们可以删除第一个数据集('df1')的'County'列中的括号()(之前的前缀,使用splitstackshape中的cSplit来拆分(,) 'County'并将数据集格式化为long,将'Accepted.Symbol'更改为'factor'类,将键列设置为'County'(setkey),加入'df2',然后dcastdata.table 的开发版本从“长”格式变为“宽”。

data.table的devel版本安装说明为here

library(data.table)#v1.9.5+
library(splitstackshape)
df1$County <- gsub('.*\\(|\\)', '', df1$County)
dcast(
   setkey(
     cSplit(df1, 'County', ',', 'long')[,
         Accepted.Symbol:= factor(Accepted.Symbol)],
          County)[df2],
    Accepted.Symbol~County, value.var='County', length, drop=FALSE)

数据

df1 <- structure(list(Accepted.Symbol = c("ABRON", "ABAM2", "ABAN", 
"ABCA", "ABFR2", "ABMA5", "ABUTI"), County = c("TX(Andrews, Armstrong, 
Bailey, Brewster)", 
"TX(Brooks, Hidalgo, Jim Hogg, Kenedy, Kleberg, Live Oak, Starr)", 
"TX(Brewster, Culberson, El Paso, Ellis, Hudspeth, Presidio, Reeves)", 
"TX(Culberson)", "TX(Andrews, Armstrong, Bailey, Briscoe)", 
"TX(Freestone, Leon, Robertson)", 
"TX(Andrews, Aransas, Atascosa, Bastrop)")), 
 .Names = c("Accepted.Symbol", 
 "County"), class = "data.frame", row.names = c(NA, -7L))

 df2 <- structure(list(County = c("Anderson", "Andrews", "Angelina", 
 "Aransas", "Archer")), .Names = "County", class = "data.frame",
 row.names = c(NA, -5L))

【讨论】:

  • 我在下载数据表的开发版本时遇到了一些问题,所以我无法对此进行测试。一旦我解决了这个问题,我会更完整地回应。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-12
  • 1970-01-01
  • 2011-10-19
  • 2022-01-25
  • 1970-01-01
  • 2018-02-27
  • 2022-01-14
相关资源
最近更新 更多