简体   繁体   English

R 操作特定序列的字符向量

[英]R manipulation a character vector for a specific sequence

I have a list of item ids as below:我有一个项目 ID 列表,如下所示:

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

Considering these item ids, I would like to generate a character variable as below for three groups (G1, G2, and G3).考虑到这些项目 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]);

The logic behind the desired output is the values in Slope[] should be 0,1, and 3 for three groups.所需的 output 背后的逻辑是Slope[]中的值对于三个组应该是0,1, and 3 and Intercept[] values should be 0 for three times for three groups.三组的三次Intercept[]值应为0

Did anyone have anything similar before?以前有没有人有类似的东西?

Thanks!谢谢!

I believe this is what you want.我相信这就是你想要的。 I have made a function for that:我为此制作了一个 function:

#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)

It produces:它产生:

     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

It can also work for other setups:它也适用于其他设置:

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

Update: I have updated the function to create a dataframe with similar structure to what you want:更新:我已经更新了 function 以创建一个 dataframe,其结构与您想要的类似:

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)
}

It will work like this:它会像这样工作:

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

Here what I figured out.这是我想出来的。

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)}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM