How to make a data frame with function in R?












0















For some basic publications I have to make almost same codes for many tables. So I have to make a quite fast code to make data frames from files and to make some same operations with data using only one same formula.



Example:



# Creating function
basic_sum <- function (place, DF, factor_col, sum) {

# Uploading data.frame
DF <- read.csv (place, sep = ";")

# Converting to factor
for (i in factor_col) {
DF [, i] <- as.factor (DF [, i])
}

# Summary
sum <- summary (DF)
View (sum)
}


Than I'm running that code and get a function basic_sum



If I want to work with my Data I call this function with arguments:



basic_sum (place = "~/DataFrame.csv", DF = DataFrame,
factor_col = c (1, 6 : 11), sum = DF_sum)


After running it nothing happens. I mean, I don't have anything new in Environment. No new data, no new vars or something else.



In my thoughts it seems that finally I have to get:



1) data.frame "DataFrame", that was uploaded DataFrame.csv;



2) 1st, 6th, 7th and all other columns until 11th will be factor



3) data.frame "DF_sum" with summary of all my columns from "DataFrame"



4) I will see data.frame "DF_sum".



Well, I see all of it in console, but I need it in Environment and to save it somewhere.



Seems that I'm doing something wrong... But I don't know what.



P.S.: If I try to run it without function (of course replacing DF to DataFrame, factor_col to с (1, 6 : 11) and so on...) everything is all right. But I have to rewrite code every time or at lest replace all DF and other that bother me.



With great regards,



Dmitrii










share|improve this question


















  • 4





    Try adding return(sum) as the last line of the function. R will return the last line of a function by default, and right now the last line is View, which does not return any values.

    – jdobres
    Nov 24 '18 at 22:55











  • Thats so sad, but nothing has happened. I mean nothing new. I still see sum in Console and nothing new in Environment

    – Dima Petrovskii
    Nov 27 '18 at 12:39


















0















For some basic publications I have to make almost same codes for many tables. So I have to make a quite fast code to make data frames from files and to make some same operations with data using only one same formula.



Example:



# Creating function
basic_sum <- function (place, DF, factor_col, sum) {

# Uploading data.frame
DF <- read.csv (place, sep = ";")

# Converting to factor
for (i in factor_col) {
DF [, i] <- as.factor (DF [, i])
}

# Summary
sum <- summary (DF)
View (sum)
}


Than I'm running that code and get a function basic_sum



If I want to work with my Data I call this function with arguments:



basic_sum (place = "~/DataFrame.csv", DF = DataFrame,
factor_col = c (1, 6 : 11), sum = DF_sum)


After running it nothing happens. I mean, I don't have anything new in Environment. No new data, no new vars or something else.



In my thoughts it seems that finally I have to get:



1) data.frame "DataFrame", that was uploaded DataFrame.csv;



2) 1st, 6th, 7th and all other columns until 11th will be factor



3) data.frame "DF_sum" with summary of all my columns from "DataFrame"



4) I will see data.frame "DF_sum".



Well, I see all of it in console, but I need it in Environment and to save it somewhere.



Seems that I'm doing something wrong... But I don't know what.



P.S.: If I try to run it without function (of course replacing DF to DataFrame, factor_col to с (1, 6 : 11) and so on...) everything is all right. But I have to rewrite code every time or at lest replace all DF and other that bother me.



With great regards,



Dmitrii










share|improve this question


















  • 4





    Try adding return(sum) as the last line of the function. R will return the last line of a function by default, and right now the last line is View, which does not return any values.

    – jdobres
    Nov 24 '18 at 22:55











  • Thats so sad, but nothing has happened. I mean nothing new. I still see sum in Console and nothing new in Environment

    – Dima Petrovskii
    Nov 27 '18 at 12:39
















0












0








0








For some basic publications I have to make almost same codes for many tables. So I have to make a quite fast code to make data frames from files and to make some same operations with data using only one same formula.



Example:



# Creating function
basic_sum <- function (place, DF, factor_col, sum) {

# Uploading data.frame
DF <- read.csv (place, sep = ";")

# Converting to factor
for (i in factor_col) {
DF [, i] <- as.factor (DF [, i])
}

# Summary
sum <- summary (DF)
View (sum)
}


Than I'm running that code and get a function basic_sum



If I want to work with my Data I call this function with arguments:



basic_sum (place = "~/DataFrame.csv", DF = DataFrame,
factor_col = c (1, 6 : 11), sum = DF_sum)


After running it nothing happens. I mean, I don't have anything new in Environment. No new data, no new vars or something else.



In my thoughts it seems that finally I have to get:



1) data.frame "DataFrame", that was uploaded DataFrame.csv;



2) 1st, 6th, 7th and all other columns until 11th will be factor



3) data.frame "DF_sum" with summary of all my columns from "DataFrame"



4) I will see data.frame "DF_sum".



Well, I see all of it in console, but I need it in Environment and to save it somewhere.



Seems that I'm doing something wrong... But I don't know what.



P.S.: If I try to run it without function (of course replacing DF to DataFrame, factor_col to с (1, 6 : 11) and so on...) everything is all right. But I have to rewrite code every time or at lest replace all DF and other that bother me.



With great regards,



Dmitrii










share|improve this question














For some basic publications I have to make almost same codes for many tables. So I have to make a quite fast code to make data frames from files and to make some same operations with data using only one same formula.



Example:



# Creating function
basic_sum <- function (place, DF, factor_col, sum) {

# Uploading data.frame
DF <- read.csv (place, sep = ";")

# Converting to factor
for (i in factor_col) {
DF [, i] <- as.factor (DF [, i])
}

# Summary
sum <- summary (DF)
View (sum)
}


Than I'm running that code and get a function basic_sum



If I want to work with my Data I call this function with arguments:



basic_sum (place = "~/DataFrame.csv", DF = DataFrame,
factor_col = c (1, 6 : 11), sum = DF_sum)


After running it nothing happens. I mean, I don't have anything new in Environment. No new data, no new vars or something else.



In my thoughts it seems that finally I have to get:



1) data.frame "DataFrame", that was uploaded DataFrame.csv;



2) 1st, 6th, 7th and all other columns until 11th will be factor



3) data.frame "DF_sum" with summary of all my columns from "DataFrame"



4) I will see data.frame "DF_sum".



Well, I see all of it in console, but I need it in Environment and to save it somewhere.



Seems that I'm doing something wrong... But I don't know what.



P.S.: If I try to run it without function (of course replacing DF to DataFrame, factor_col to с (1, 6 : 11) and so on...) everything is all right. But I have to rewrite code every time or at lest replace all DF and other that bother me.



With great regards,



Dmitrii







r function dataframe






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 24 '18 at 22:52









Dima PetrovskiiDima Petrovskii

1




1








  • 4





    Try adding return(sum) as the last line of the function. R will return the last line of a function by default, and right now the last line is View, which does not return any values.

    – jdobres
    Nov 24 '18 at 22:55











  • Thats so sad, but nothing has happened. I mean nothing new. I still see sum in Console and nothing new in Environment

    – Dima Petrovskii
    Nov 27 '18 at 12:39
















  • 4





    Try adding return(sum) as the last line of the function. R will return the last line of a function by default, and right now the last line is View, which does not return any values.

    – jdobres
    Nov 24 '18 at 22:55











  • Thats so sad, but nothing has happened. I mean nothing new. I still see sum in Console and nothing new in Environment

    – Dima Petrovskii
    Nov 27 '18 at 12:39










4




4





Try adding return(sum) as the last line of the function. R will return the last line of a function by default, and right now the last line is View, which does not return any values.

– jdobres
Nov 24 '18 at 22:55





Try adding return(sum) as the last line of the function. R will return the last line of a function by default, and right now the last line is View, which does not return any values.

– jdobres
Nov 24 '18 at 22:55













Thats so sad, but nothing has happened. I mean nothing new. I still see sum in Console and nothing new in Environment

– Dima Petrovskii
Nov 27 '18 at 12:39







Thats so sad, but nothing has happened. I mean nothing new. I still see sum in Console and nothing new in Environment

– Dima Petrovskii
Nov 27 '18 at 12:39














0






active

oldest

votes











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%2f53463059%2fhow-to-make-a-data-frame-with-function-in-r%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















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%2f53463059%2fhow-to-make-a-data-frame-with-function-in-r%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

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks