How to add sets to the dataframe by loop?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







-1















I have some sets which contain numbers and named as k1,k2,k3,k4 ... k20. Now I want to make a dataframe base on them. How can I do it will loop? My present thought is



k1 <- c(1,2,3)
k2 <- c(2,3,4)
k3 <- c(4,5,6)
...
k20<- c(3,4,5)
i <- 1
df1 <- c()
which (i <= parameter){
df1 <- dataframe(df1,ki)
i = i+1
}
#What I want to do is build different size of dataframe by given a parameter
#when parameter = 15 then df1 <- dataframe (k1,k2,k3...,k15)
# when parameter = 12 then df1 <- dataframe (k1,k2,k3...,k12)


But it did not work. Could anyone give me some tips on how can I do that? Thanks










share|improve this question

























  • Any reason why rbind doesn't work?

    – Vivek Kalyanarangan
    Jan 3 at 12:32











  • @VivekKalyanarangan mean problem is ki do not work

    – Codezy
    Jan 3 at 12:33











  • What is ki in this case?

    – Vivek Kalyanarangan
    Jan 3 at 12:35













  • I think they are looking for data.frame(var = paste0("k",1:15))

    – Jake Kaupp
    Jan 3 at 12:38













  • @RonakShah Sorry I did not make it clean. I have edited my question now. Please tell me if I make it clean this time. thank you

    – Codezy
    Jan 3 at 12:40


















-1















I have some sets which contain numbers and named as k1,k2,k3,k4 ... k20. Now I want to make a dataframe base on them. How can I do it will loop? My present thought is



k1 <- c(1,2,3)
k2 <- c(2,3,4)
k3 <- c(4,5,6)
...
k20<- c(3,4,5)
i <- 1
df1 <- c()
which (i <= parameter){
df1 <- dataframe(df1,ki)
i = i+1
}
#What I want to do is build different size of dataframe by given a parameter
#when parameter = 15 then df1 <- dataframe (k1,k2,k3...,k15)
# when parameter = 12 then df1 <- dataframe (k1,k2,k3...,k12)


But it did not work. Could anyone give me some tips on how can I do that? Thanks










share|improve this question

























  • Any reason why rbind doesn't work?

    – Vivek Kalyanarangan
    Jan 3 at 12:32











  • @VivekKalyanarangan mean problem is ki do not work

    – Codezy
    Jan 3 at 12:33











  • What is ki in this case?

    – Vivek Kalyanarangan
    Jan 3 at 12:35













  • I think they are looking for data.frame(var = paste0("k",1:15))

    – Jake Kaupp
    Jan 3 at 12:38













  • @RonakShah Sorry I did not make it clean. I have edited my question now. Please tell me if I make it clean this time. thank you

    – Codezy
    Jan 3 at 12:40














-1












-1








-1








I have some sets which contain numbers and named as k1,k2,k3,k4 ... k20. Now I want to make a dataframe base on them. How can I do it will loop? My present thought is



k1 <- c(1,2,3)
k2 <- c(2,3,4)
k3 <- c(4,5,6)
...
k20<- c(3,4,5)
i <- 1
df1 <- c()
which (i <= parameter){
df1 <- dataframe(df1,ki)
i = i+1
}
#What I want to do is build different size of dataframe by given a parameter
#when parameter = 15 then df1 <- dataframe (k1,k2,k3...,k15)
# when parameter = 12 then df1 <- dataframe (k1,k2,k3...,k12)


But it did not work. Could anyone give me some tips on how can I do that? Thanks










share|improve this question
















I have some sets which contain numbers and named as k1,k2,k3,k4 ... k20. Now I want to make a dataframe base on them. How can I do it will loop? My present thought is



k1 <- c(1,2,3)
k2 <- c(2,3,4)
k3 <- c(4,5,6)
...
k20<- c(3,4,5)
i <- 1
df1 <- c()
which (i <= parameter){
df1 <- dataframe(df1,ki)
i = i+1
}
#What I want to do is build different size of dataframe by given a parameter
#when parameter = 15 then df1 <- dataframe (k1,k2,k3...,k15)
# when parameter = 12 then df1 <- dataframe (k1,k2,k3...,k12)


But it did not work. Could anyone give me some tips on how can I do that? Thanks







r loops dataframe






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 13:35







Codezy

















asked Jan 3 at 12:30









CodezyCodezy

18210




18210













  • Any reason why rbind doesn't work?

    – Vivek Kalyanarangan
    Jan 3 at 12:32











  • @VivekKalyanarangan mean problem is ki do not work

    – Codezy
    Jan 3 at 12:33











  • What is ki in this case?

    – Vivek Kalyanarangan
    Jan 3 at 12:35













  • I think they are looking for data.frame(var = paste0("k",1:15))

    – Jake Kaupp
    Jan 3 at 12:38













  • @RonakShah Sorry I did not make it clean. I have edited my question now. Please tell me if I make it clean this time. thank you

    – Codezy
    Jan 3 at 12:40



















  • Any reason why rbind doesn't work?

    – Vivek Kalyanarangan
    Jan 3 at 12:32











  • @VivekKalyanarangan mean problem is ki do not work

    – Codezy
    Jan 3 at 12:33











  • What is ki in this case?

    – Vivek Kalyanarangan
    Jan 3 at 12:35













  • I think they are looking for data.frame(var = paste0("k",1:15))

    – Jake Kaupp
    Jan 3 at 12:38













  • @RonakShah Sorry I did not make it clean. I have edited my question now. Please tell me if I make it clean this time. thank you

    – Codezy
    Jan 3 at 12:40

















Any reason why rbind doesn't work?

– Vivek Kalyanarangan
Jan 3 at 12:32





Any reason why rbind doesn't work?

– Vivek Kalyanarangan
Jan 3 at 12:32













@VivekKalyanarangan mean problem is ki do not work

– Codezy
Jan 3 at 12:33





@VivekKalyanarangan mean problem is ki do not work

– Codezy
Jan 3 at 12:33













What is ki in this case?

– Vivek Kalyanarangan
Jan 3 at 12:35







What is ki in this case?

– Vivek Kalyanarangan
Jan 3 at 12:35















I think they are looking for data.frame(var = paste0("k",1:15))

– Jake Kaupp
Jan 3 at 12:38







I think they are looking for data.frame(var = paste0("k",1:15))

– Jake Kaupp
Jan 3 at 12:38















@RonakShah Sorry I did not make it clean. I have edited my question now. Please tell me if I make it clean this time. thank you

– Codezy
Jan 3 at 12:40





@RonakShah Sorry I did not make it clean. I have edited my question now. Please tell me if I make it clean this time. thank you

– Codezy
Jan 3 at 12:40












1 Answer
1






active

oldest

votes


















2














You can use ls with pattern to get objects which start with k and then subset the objects using 1:n, use mixedsort to sort the vectors according to their names (to avoid k11 being picked up before k2) and put all this one dataframe.



library(gtools)

n = 5
object_name <- mixedsort(ls(pattern = "^k"))[1:n]
data.frame(mget(object_name))

# k1 k2 k3 k4 k5
#1 1 2 4 3 3
#2 2 3 5 4 4
#3 3 4 6 5 5

n = 2
object_name <- mixedsort(ls(pattern = "^k"))[1:n]
data.frame(mget(object_name))

# k1 k2
#1 1 2
#2 2 3
#3 3 4


data



k1 <- c(1,2,3)
k2 <- c(2,3,4)
k3 <- c(4,5,6)
k4 <- c(3, 4, 5)
k5 <- c(3,4,5)





share|improve this answer


























  • Thank you for answering. Could you tell me the meaning of ls? And how can I control the number of ki I want to add into the df?

    – Codezy
    Jan 3 at 12:51











  • @Codezy ls actually returns characters strings of all the objects in your environment. just type ls() in your console and see what you get. With ls(pattern = "^k") it will return objects which only start with k.

    – Ronak Shah
    Jan 3 at 13:00













  • ok actually I want to make a function and control the size of my dataframe by giving the parameter. Do you know how to do that?

    – Codezy
    Jan 3 at 13:02











  • @Codezy I am not sure if I understand you completely. What do you mean by control the size of my dataframe by giving the parameter ?

    – Ronak Shah
    Jan 3 at 13:07













  • I got 20 sets which are k1 to k20, I want to make different size dataframes to only contain for example k1 to k5 or k1 to k16.

    – Codezy
    Jan 3 at 13:09












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54022340%2fhow-to-add-sets-to-the-dataframe-by-loop%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














You can use ls with pattern to get objects which start with k and then subset the objects using 1:n, use mixedsort to sort the vectors according to their names (to avoid k11 being picked up before k2) and put all this one dataframe.



library(gtools)

n = 5
object_name <- mixedsort(ls(pattern = "^k"))[1:n]
data.frame(mget(object_name))

# k1 k2 k3 k4 k5
#1 1 2 4 3 3
#2 2 3 5 4 4
#3 3 4 6 5 5

n = 2
object_name <- mixedsort(ls(pattern = "^k"))[1:n]
data.frame(mget(object_name))

# k1 k2
#1 1 2
#2 2 3
#3 3 4


data



k1 <- c(1,2,3)
k2 <- c(2,3,4)
k3 <- c(4,5,6)
k4 <- c(3, 4, 5)
k5 <- c(3,4,5)





share|improve this answer


























  • Thank you for answering. Could you tell me the meaning of ls? And how can I control the number of ki I want to add into the df?

    – Codezy
    Jan 3 at 12:51











  • @Codezy ls actually returns characters strings of all the objects in your environment. just type ls() in your console and see what you get. With ls(pattern = "^k") it will return objects which only start with k.

    – Ronak Shah
    Jan 3 at 13:00













  • ok actually I want to make a function and control the size of my dataframe by giving the parameter. Do you know how to do that?

    – Codezy
    Jan 3 at 13:02











  • @Codezy I am not sure if I understand you completely. What do you mean by control the size of my dataframe by giving the parameter ?

    – Ronak Shah
    Jan 3 at 13:07













  • I got 20 sets which are k1 to k20, I want to make different size dataframes to only contain for example k1 to k5 or k1 to k16.

    – Codezy
    Jan 3 at 13:09
















2














You can use ls with pattern to get objects which start with k and then subset the objects using 1:n, use mixedsort to sort the vectors according to their names (to avoid k11 being picked up before k2) and put all this one dataframe.



library(gtools)

n = 5
object_name <- mixedsort(ls(pattern = "^k"))[1:n]
data.frame(mget(object_name))

# k1 k2 k3 k4 k5
#1 1 2 4 3 3
#2 2 3 5 4 4
#3 3 4 6 5 5

n = 2
object_name <- mixedsort(ls(pattern = "^k"))[1:n]
data.frame(mget(object_name))

# k1 k2
#1 1 2
#2 2 3
#3 3 4


data



k1 <- c(1,2,3)
k2 <- c(2,3,4)
k3 <- c(4,5,6)
k4 <- c(3, 4, 5)
k5 <- c(3,4,5)





share|improve this answer


























  • Thank you for answering. Could you tell me the meaning of ls? And how can I control the number of ki I want to add into the df?

    – Codezy
    Jan 3 at 12:51











  • @Codezy ls actually returns characters strings of all the objects in your environment. just type ls() in your console and see what you get. With ls(pattern = "^k") it will return objects which only start with k.

    – Ronak Shah
    Jan 3 at 13:00













  • ok actually I want to make a function and control the size of my dataframe by giving the parameter. Do you know how to do that?

    – Codezy
    Jan 3 at 13:02











  • @Codezy I am not sure if I understand you completely. What do you mean by control the size of my dataframe by giving the parameter ?

    – Ronak Shah
    Jan 3 at 13:07













  • I got 20 sets which are k1 to k20, I want to make different size dataframes to only contain for example k1 to k5 or k1 to k16.

    – Codezy
    Jan 3 at 13:09














2












2








2







You can use ls with pattern to get objects which start with k and then subset the objects using 1:n, use mixedsort to sort the vectors according to their names (to avoid k11 being picked up before k2) and put all this one dataframe.



library(gtools)

n = 5
object_name <- mixedsort(ls(pattern = "^k"))[1:n]
data.frame(mget(object_name))

# k1 k2 k3 k4 k5
#1 1 2 4 3 3
#2 2 3 5 4 4
#3 3 4 6 5 5

n = 2
object_name <- mixedsort(ls(pattern = "^k"))[1:n]
data.frame(mget(object_name))

# k1 k2
#1 1 2
#2 2 3
#3 3 4


data



k1 <- c(1,2,3)
k2 <- c(2,3,4)
k3 <- c(4,5,6)
k4 <- c(3, 4, 5)
k5 <- c(3,4,5)





share|improve this answer















You can use ls with pattern to get objects which start with k and then subset the objects using 1:n, use mixedsort to sort the vectors according to their names (to avoid k11 being picked up before k2) and put all this one dataframe.



library(gtools)

n = 5
object_name <- mixedsort(ls(pattern = "^k"))[1:n]
data.frame(mget(object_name))

# k1 k2 k3 k4 k5
#1 1 2 4 3 3
#2 2 3 5 4 4
#3 3 4 6 5 5

n = 2
object_name <- mixedsort(ls(pattern = "^k"))[1:n]
data.frame(mget(object_name))

# k1 k2
#1 1 2
#2 2 3
#3 3 4


data



k1 <- c(1,2,3)
k2 <- c(2,3,4)
k3 <- c(4,5,6)
k4 <- c(3, 4, 5)
k5 <- c(3,4,5)






share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 3 at 13:59

























answered Jan 3 at 12:42









Ronak ShahRonak Shah

46.6k104269




46.6k104269













  • Thank you for answering. Could you tell me the meaning of ls? And how can I control the number of ki I want to add into the df?

    – Codezy
    Jan 3 at 12:51











  • @Codezy ls actually returns characters strings of all the objects in your environment. just type ls() in your console and see what you get. With ls(pattern = "^k") it will return objects which only start with k.

    – Ronak Shah
    Jan 3 at 13:00













  • ok actually I want to make a function and control the size of my dataframe by giving the parameter. Do you know how to do that?

    – Codezy
    Jan 3 at 13:02











  • @Codezy I am not sure if I understand you completely. What do you mean by control the size of my dataframe by giving the parameter ?

    – Ronak Shah
    Jan 3 at 13:07













  • I got 20 sets which are k1 to k20, I want to make different size dataframes to only contain for example k1 to k5 or k1 to k16.

    – Codezy
    Jan 3 at 13:09



















  • Thank you for answering. Could you tell me the meaning of ls? And how can I control the number of ki I want to add into the df?

    – Codezy
    Jan 3 at 12:51











  • @Codezy ls actually returns characters strings of all the objects in your environment. just type ls() in your console and see what you get. With ls(pattern = "^k") it will return objects which only start with k.

    – Ronak Shah
    Jan 3 at 13:00













  • ok actually I want to make a function and control the size of my dataframe by giving the parameter. Do you know how to do that?

    – Codezy
    Jan 3 at 13:02











  • @Codezy I am not sure if I understand you completely. What do you mean by control the size of my dataframe by giving the parameter ?

    – Ronak Shah
    Jan 3 at 13:07













  • I got 20 sets which are k1 to k20, I want to make different size dataframes to only contain for example k1 to k5 or k1 to k16.

    – Codezy
    Jan 3 at 13:09

















Thank you for answering. Could you tell me the meaning of ls? And how can I control the number of ki I want to add into the df?

– Codezy
Jan 3 at 12:51





Thank you for answering. Could you tell me the meaning of ls? And how can I control the number of ki I want to add into the df?

– Codezy
Jan 3 at 12:51













@Codezy ls actually returns characters strings of all the objects in your environment. just type ls() in your console and see what you get. With ls(pattern = "^k") it will return objects which only start with k.

– Ronak Shah
Jan 3 at 13:00







@Codezy ls actually returns characters strings of all the objects in your environment. just type ls() in your console and see what you get. With ls(pattern = "^k") it will return objects which only start with k.

– Ronak Shah
Jan 3 at 13:00















ok actually I want to make a function and control the size of my dataframe by giving the parameter. Do you know how to do that?

– Codezy
Jan 3 at 13:02





ok actually I want to make a function and control the size of my dataframe by giving the parameter. Do you know how to do that?

– Codezy
Jan 3 at 13:02













@Codezy I am not sure if I understand you completely. What do you mean by control the size of my dataframe by giving the parameter ?

– Ronak Shah
Jan 3 at 13:07







@Codezy I am not sure if I understand you completely. What do you mean by control the size of my dataframe by giving the parameter ?

– Ronak Shah
Jan 3 at 13:07















I got 20 sets which are k1 to k20, I want to make different size dataframes to only contain for example k1 to k5 or k1 to k16.

– Codezy
Jan 3 at 13:09





I got 20 sets which are k1 to k20, I want to make different size dataframes to only contain for example k1 to k5 or k1 to k16.

– Codezy
Jan 3 at 13:09




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54022340%2fhow-to-add-sets-to-the-dataframe-by-loop%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

SQL update select statement

'app-layout' is not a known element: how to share Component with different Modules