R - 如何向数据框添加新列,该数据框是另一列的计算结果

时间:2016-08-04 18:57:24

标签: r

我正在尝试在R中编写一个将输入信息映射到输出的函数,但是将输入保留在同一个数据框中。我有一个函数,我写的将接受输入并为单个值提供正确的输出,但我缺乏R知识知道如何重写这个,所以我可以传入一个向量。

我想在一个框架中传递它,并向数据框添加一个新列。示例代码如下:

d <- data.frame(Type=c("_Swap_","Nothing","CAP", "FLOOR", "FLOOR", "BLAH", "Digital", "Something!"))

d$newType= createTags(d$Type)

createTags = function(Type){
   map = data.frame(input=c("Swap","Note","Option", "Floor", "FLOOR", "CAP", "Digital", "Bond"), 
                  output=c("Swap","Note","Option", "CapFloor", "CapFloor", "CapFloor", "CapFloor", "Bond"))

   tag="Unknown"

   print(length(Type))

   for (j in 1:NROW(map)) {    
     input = map[j,]$input
     output = map[j,]$output

     ifelse (grepl(toupper(input), toupper(Type))){
       tag = output
     }    
   }

   return(tag)  
 }

所以在输出中我希望d有两列,一列是原始的Type,然后最后应该有一个名为newType的第二列。

2 个答案:

答案 0 :(得分:0)

您可以使用switch功能将输入映射到输出:

# Create sample data
d <- data.frame(
  type = c("_Swap_","Nothing","CAP", "FLOOR", "FLOOR", "BLAH", "Digital", "Something!")
)

# Use the switch function to map inputs to outputs
create_tags <- function(type) {
  type <- gsub("[[:punct:]]", "", tolower(type)) # standardize the letter case and remove punctuation
  switch(
    type,
    "swap"    = "Swap",
    "note"    = "Note",
    "option"  = "Option",
    "floor"   = "CapFloor",
    "cap"     = "CapFloor",
    "digital" = "CapFloor", 
    "bond"    = "Bond",
    "Unknown"
  )
}  

# Switch isn't vectorized so we have to sapply our create_tags function onto d$type
d$new_type <- sapply(d$type, create_tags)

答案 1 :(得分:0)

我最后玩弄了很多。感谢Mark Timms的帮助,虽然我意识到我需要使用grepl来确保我可以覆盖所有案例

SRTLondon <- 
   data.frame(Type=c("_Swap_","Nothing","CAP", "FLOOR", "FLOOR", "BLAH", "Digital", "Something!"))

createTags = function(Type){
   #browser()
   map = data.frame(input=c("Swap","Note","Option", "Floor", "FLOOR", "CAP", "Digital", "Bond"), 
               output=c("Swap","Note","Option", "CapFloor", "CapFloor", "CapFloor", "CapFloor", "Bond"),stringsAsFactors = F)

   work = data.frame(x=toupper(Type),stringsAsFactors = F)
   work$tag="NA"

   for (i in 1:NROW(work)) {  
    check = work$x[i]

    for (k in 1:nrow(map)){
      input = map[k,'input']
      output = map[k,'output']

      if(grepl(toupper(input),check))
      {
        work$tag[i]=output
        break
      }

    }

  }
  return(work$tag)
}

SRTLondon $ type.tags = createTags(SRTLondon $ Type)