R manipulation a character vector for a specific sequence

扶醉桌前 提交于 2021-02-11 12:34:53

问题


I have a list of item ids as below:

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

    #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. and Intercept[] values should be 0 for three times for three groups.

Did anyone have anything similar before?

Thanks!


回答1:


I believe this is what you want. I have made a function for that:

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

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



回答2:


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


来源:https://stackoverflow.com/questions/63143023/r-manipulation-a-character-vector-for-a-specific-sequence

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!