0

I have a list of sets stored in variable data.I want to assign these sets separately to variables as below.

q1_set1_m <- data[["q1_set1_proj1"]]
q1_set2_m <- data[["q1_set2_proj1"]]
q1_set3_m <- data[["q2_set3_proj1"]]
q2_set1_m <- data[["q2_set1_proj1"]]
q2_set2_m <- data[["q2_set2_proj1"]]
q2_set3_m <- data[["q2_set3_proj1""]]
q3_set1_m <- data[["q3_set1_proj1""]]
q3_set2_m <- data[["q3_set2_proj1""]]
q3_set3_m <- data[["q3_set3_proj1""]]

Since the variable data has many sets so I tried to do this in for loop Method 1

  for(qNum in 1:3){
    for(setNum in 1:3){
                s1=as.character("[")
                s2=as.character("]")
                print(paste0("q_",qNum,"_set",setNum,"_m=","data",as.character(s1),as.character(s1),"q",qNum,"_",setNum,"_proj1",as.character(s2),as.character(s2)))
                }
}

Method2

for(qNum in 1:3){
    for(setNum in 1:3){
        s1=as.character("[")
        s2=as.character("]")
        var <-   paste0("=","data",as.character(s1),as.character(s1),"q",qNum,"_",setNum,"_proj1",as.character(s2),as.character(s2))

        assign(paste0("q_",qNum,"_set",setNum,"_m"), var)    
     }

}

data

 str(data)
 List of 40
  $ merged_q1_set1_proj1:'data.frame':  1959931 obs. of  2 variables:
  ..$ V1: Factor w/ 54577 levels "XLOC_000001",..: 1 1 1 1 1 1 1 1 1 1 ...
  ..$ V2: Factor w/ 50648 levels "5S_rRNA","7SK",..: 14964 14964 14964 14964 14964 14964 14964   14964 14964 14964 ...
  $ merged_q1_set2_proj1:'data.frame':  1959267 obs. of  2 variables:
  ..$ V1: Factor w/ 53423 levels "XLOC_000001",..: 1 1 1 1 1 1 1 1 1 1 ...
  ..$ V2: Factor w/ 50633 levels "5S_rRNA","7SK",..: 14949 14949 14949 14949 14949 14949 14949 14949 14949 14949 ...
   $ merged_q2_set1_proj1:'data.frame': 1956746 obs. of  2 variables:
   .......

This can only print the variable names and data sets. I am not sure how to assign the data set to different variables.

Thanks

11
  • 1
    May be you can try assign. Commented Sep 22, 2014 at 12:36
  • yes I did and I got the same printed names Commented Sep 22, 2014 at 12:38
  • 1
    Perhaps a reproducible example data and expected result would help others to check Commented Sep 22, 2014 at 12:39
  • 2
    Why assign? What is the next step? Commented Sep 22, 2014 at 12:40
  • 1
    Try: get("q_1_set1_m") = get("data[[q1_1_proj1]]") Commented Sep 22, 2014 at 12:47

1 Answer 1

1

If data is a data.frame, you could try (although assign is not recommended):

set.seed(42)
dat1 <- as.data.frame(matrix(sample(1:25, 9*10, replace=TRUE), ncol=9)) 

colnames(dat1) <- paste(rep(paste0("q", 1:3), c(2,4,3)), rep(paste0("set",1:3), 3), "proj1", sep="_")
names1 <- paste(rep(paste0("q", 1:3), c(2,4,3)), rep(paste0("set", 1:3), 3), "m", sep="_") #if `names1` changes only at the last part,
#it could be changed easily in other ways.  But, I am using `paste` in case you want to delete/add extra things.

for(i in seq_along(names1)){ # looping over the names
    assign(names1[i], dat1[,i])
}

q1_set1_m
#[1] 23 24  8 21 17 13 19  4 17 18

q1_set2_m
#[1] 12 18 24  7 12 24 25  3 12 15

 dat1[,1]
 #[1] 23 24  8 21 17 13 19  4 17 18

Update

If you have a list

 lst1 <- as.list(dat1)
 for(i in seq_along(names1)){
 assign(names1[i], lst1[[i]])
 }

q1_set1_m
#[1] 23 24  8 21 17 13 19  4 17 18
Sign up to request clarification or add additional context in comments.

2 Comments

Why is assign not recommended?
@skan In general, it is better not to create multiple objects in the global environment. You can do all the operations within a list itsefl

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.