【问题标题】:R manipulation a character vector for a specific sequenceR 操作特定序列的字符向量
【发布时间】:2020-07-28 21:27:52
【问题描述】:

我有一个项目 ID 列表如下:

ids <- c("12_a","23_b")

考虑到这些项目 ID,我想为三个组(G1、G2 和 G3)生成如下字符变量。

    #for the first item
Equal = (G1,12_a, Slope[0]), 
        (G2,12_a, Slope[0]), 
        (G3,12_a, Slope[0]);
Equal = (G1,12_a, Slope[1]), 
        (G2,12_a, Slope[1]), 
        (G3,12_a, Slope[1]);
Equal = (G1,12_a, Slope[2]), 
        (G2,12_a, Slope[2]), 
        (G3,12_a, Slope[2]);
Equal = (G1,12_a, Intercept[0]), 
        (G2,12_a, Intercept[0]), 
        (G3,12_a, Intercept[0]);
    
    #for the second item
Equal = (G1,23_b, Slope[0]), 
        (G2,23_b, Slope[0]), 
        (G3,23_b, Slope[0]);
Equal = (G1,23_b, Slope[1]), 
        (G2,23_b, Slope[1]), 
        (G3,23_b, Slope[1]);
Equal = (G1,23_b, Slope[2]), 
        (G2,23_b, Slope[2]), 
        (G3,23_b, Slope[2]);
Equal = (G1,23_b, Intercept[0]), 
        (G2,23_b, Intercept[0]), 
        (G3,23_b, Intercept[0]);

所需输出背后的逻辑是Slope[] 中的值对于三个组应该是0,1, and 3。并且Intercept[] 的值应该是0 三倍于三组。

以前有人有类似的吗?

谢谢!

【问题讨论】:

    标签: r character


    【解决方案1】:

    我相信这就是你想要的。我为此做了一个函数:

    #Vector of ids
    ids <- c("12_a","23_b")
    #Function to create data
    #x is a vector of ids
    #n is the number of times you want data repeated according to your specifications
    create <- function(x,n)
    {
      #Build vars
      groups <- rep(paste0('Group_',1:n),n)
      intercept <- rep(rep(0,n),n) 
      slope <- do.call(c,lapply(0:(n-1),rep,n))
      df <- data.frame(groups,intercept,slope)
      #Feed ids
      index <- length(x)
      #Use a loop
      List <- list()
      for(i in 1:index)
      {
        List[[i]] <- data.frame(id=x[i],df)
      }
      #Create final object
      DF <- do.call(rbind,List)
      return(DF)
    }
    #Use the function
    create(x = ids,n = 3)
    

    它产生:

         id  groups intercept slope
    1  12_a Group_1         0     0
    2  12_a Group_2         0     0
    3  12_a Group_3         0     0
    4  12_a Group_1         0     1
    5  12_a Group_2         0     1
    6  12_a Group_3         0     1
    7  12_a Group_1         0     2
    8  12_a Group_2         0     2
    9  12_a Group_3         0     2
    10 23_b Group_1         0     0
    11 23_b Group_2         0     0
    12 23_b Group_3         0     0
    13 23_b Group_1         0     1
    14 23_b Group_2         0     1
    15 23_b Group_3         0     1
    16 23_b Group_1         0     2
    17 23_b Group_2         0     2
    18 23_b Group_3         0     2
    

    它也适用于其他设置:

    #Another test
    ids2 <- ids <- c("12_a","23_b","65_c")
    create(x = ids2,n = 5)
    

    更新: 我已经更新了函数来创建一个与你想要的结构相似的数据框:

    create <- function(x,n)
    {
      #Build vars
      groups <- rep(paste0('Group_',1:n),n)
      intercept <- data.frame(groups=paste0('Group_',1:n),var='intercept',val=0) 
      val <- do.call(c,lapply(0:(n-1),rep,n))
      df <- data.frame(groups,var='slope',val)
      #Bind all
      dfm <- rbind(df,intercept)
      #Feed ids
      index <- length(x)
      #Use a loop
      List <- list()
      for(i in 1:index)
      {
        List[[i]] <- data.frame(id=x[i],dfm)
      }
      #Create final object
      DF <- do.call(rbind,List)
      return(DF)
    }
    

    它将像这样工作:

    DF <- create(x = ids,n = 3)
    
         id  groups       var val
    1  12_a Group_1     slope   0
    2  12_a Group_2     slope   0
    3  12_a Group_3     slope   0
    4  12_a Group_1     slope   1
    5  12_a Group_2     slope   1
    6  12_a Group_3     slope   1
    7  12_a Group_1     slope   2
    8  12_a Group_2     slope   2
    9  12_a Group_3     slope   2
    10 12_a Group_1 intercept   0
    11 12_a Group_2 intercept   0
    12 12_a Group_3 intercept   0
    13 23_b Group_1     slope   0
    14 23_b Group_2     slope   0
    15 23_b Group_3     slope   0
    16 23_b Group_1     slope   1
    17 23_b Group_2     slope   1
    18 23_b Group_3     slope   1
    19 23_b Group_1     slope   2
    20 23_b Group_2     slope   2
    21 23_b Group_3     slope   2
    22 23_b Group_1 intercept   0
    23 23_b Group_2 intercept   0
    24 23_b Group_3 intercept   0
    

    【讨论】:

    • @amisos55 是的,您只需分配给一个新对象,例如mydf &lt;- create(x = ids,n = 3) 让我知道它是否有效。
    • 有没有办法将此结构转换为我想要的输出中的字符值?
    • @amisos55 我已经更新了函数以生成与您想要的结构相似的数据帧。让我知道这是否有帮助。
    【解决方案2】:

    这是我想出来的。

    ids <- c("12_a","23_b")
    group <- 3
    Slope.0 <- c() # store slope vector
    Intercept.0 <- c() # store intercept vector
    
    for(i in 1:length(ids)) {
    
      for(j in 0:group) { # here with the length(State) I gained the sequqnece of 0,1,2,3
        slope.0 <- paste0(paste0("Equal = ",paste0(paste("(", "G1, ",ids[i], ","," Slope[",j,"])",collapse=", ", sep=""),", ",
                                            paste( "(", "G2, ",ids[i], ","," Slope[",j,"])",collapse=", ", sep=""),", ",
                                            paste( "(", "G3, ",ids[i], ","," Slope[",j,"])",collapse=", ", sep=""))), ";")
        Slope.0 <- c(Slope.0, slope.0)
        
      }
      
      
      
      intercept.0 <- paste0(paste0("Equal = ",paste0(paste("(", "G1, ",ids[i], ","," Intercept[0])",collapse=", ", sep=""),", ",
                                              paste( "(", "G2, ",ids[i], ","," Intercept[0])",collapse=", ", sep=""),", ",
                                              paste( "(", "G3, ",ids[i], ","," Intercept[0])",collapse=", ", sep=""))),";")
      Intercept.0 <- c(Intercept.0, intercept.0)}
    

    【讨论】:

      猜你喜欢
      • 2021-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-03
      相关资源
      最近更新 更多