0

I want to change the name of the output of my R function to reflect different strings that are inputted. Here is what I have tried:

kd = c("a","b","d","e","b")

test = function(kd){

  return(list(assign(paste(kd,"burst",sep="_"),1:6)))

}

This is just a simple test function. I get the warning (which is just as bad an error for me):

Warning message:
In assign(paste(kd, "burst", sep = "_"), 1:6) :
  only the first element is used as variable name

Ideally I would get ouput like a_burst = 1, b_burst = 2 and so on but am not getting close.

I would like split up a dataframe by contents of a vector and be able to name everything according to the name from that vector, similar to

How to split a data frame by rows, and then process the blocks?

but not quite. The naming is imperative.

4

2 回答 2

3

像这样的东西,也许?

kd = c("a","b","d","e","b")

test <- function(x){
    l <- as.list(1:5)
    names(l) <- paste(x,"burst",sep = "_")
    l
}

test(kd)
于 2013-05-13T21:18:44.657 回答
2

您可以通过 setNames 使用向量而不是列表:

t1_6 <- setNames( 1:6, kd)
t1_6
   a    b    d    e    b <NA> 
   1    2    3    4    5    6 

> t1_6["a"]
a 
1 

再次查看这个问题,我想知道您是否想将顺序名称分配给字符向量:

> a1_5 <- setNames(kd, paste0("alpha", 1:5))
> a1_5
alpha1 alpha2 alpha3 alpha4 alpha5 
   "a"    "b"    "d"    "e"    "b" 
于 2013-05-13T21:26:26.630 回答