स्क्रिप्ट को फंक्शंस में बदलना

R में फ़ंक्शन लिखने का परिचय

Richie Cotton

Data Evangelist at DataCamp

एक बेसिक फंक्शन टेम्पलेट

my_fun <- function(arg1, arg2) {
  # Do something
}

सिग्नेचर

          function(arg1, arg2)

बॉडी

                               {
  # Do something
}
R में फ़ंक्शन लिखने का परिचय
library(readr)
test_scores_geography_raw <- read_csv("test_scores_geography.csv")

library(dplyr)
library(lubridate)
test_scores_geography_clean <- test_scores_geography_raw %>% 
  select(person_id, first_name, last_name, test_date, score) %>%
  mutate(test_date = mdy(test_date)) %>%
  filter(!is.na(score))
library(readr)
test_scores_english_raw <- read_csv("test_scores_english.csv")

library(dplyr)
library(lubridate)
test_scores_english_clean <- test_scores_english_raw %>% 
  select(person_id, first_name, last_name, test_date, score) %>%
  mutate(test_date = mdy(test_date)) %>%
  filter(!is.na(score))
library(readr)
test_scores_art_raw <- read_csv("test_scores_art.csv")

library(dplyr)
library(lubridate)
test_scores_art_clean <- test_scores_art_raw %>% 
  select(person_id, first_name, last_name, test_date, score) %>%
  mutate(test_date = mdy(test_date)) %>%
  filter(is.na(score))
library(readr)
test_scores_spanish_raw <- read_csv("test_scores_spanish.csv")

library(dplyr)
library(lubridate)
test_scores_spanish_clean <- test_scores_spanish_raw %>% 
  select(person_id, first_name, last_name, test_date, score) %>%
  mutate(test_date = mdy(test_date)) %>%
  filter(!is.na(score))
R में फ़ंक्शन लिखने का परिचय

एक टेम्पलेट बनाएँ

import_test_scores <- function() {






}
R में फ़ंक्शन लिखने का परिचय

अपनी स्क्रिप्ट बॉडी में पेस्ट करें

import_test_scores <- function() {
  test_scores_geography_raw <- read_csv("test_scores_geography.csv")

  test_scores_geography_clean <- test_scores_geography_raw %>% 
    select(person_id, first_name, last_name, test_date, score) %>%
    mutate(test_date = mdy(test_date)) %>%
    filter(!is.na(score))
}
R में फ़ंक्शन लिखने का परिचय

आर्ग्युमेंट चुनें

import_test_scores <- function(filename) {         # <- केवल 1 आर्ग्युमेंट
  test_scores_geography_raw <- read_csv("test_scores_geography.csv")

  test_scores_geography_clean <- test_scores_geography_raw %>% 
    select(person_id, first_name, last_name, test_date, score) %>%
    mutate(test_date = mdy(test_date)) %>%
    filter(!is.na(score))
}
R में फ़ंक्शन लिखने का परिचय

विशिष्ट मानों को आर्ग्युमेंट्स से बदलें

import_test_scores <- function(filename) {
  test_scores_geography_raw <- read_csv(filename)  # <- विशेष फ़ाइलनाम बदलें

  test_scores_geography_clean <- raw_data %>% 
    select(person_id, first_name, last_name, test_date, score) %>%
    mutate(test_date = mdy(test_date)) %>%
    filter(!is.na(score))
}
R में फ़ंक्शन लिखने का परिचय

वैरिएबल नाम सामान्य करें

import_test_scores <- function(filename) {
  test_scores_raw <- read_csv(filename)   # <- वैरिएबल नाम सामान्य करें

  test_scores_clean <- test_scores_raw %>%       # <- वैरिएबल नाम सामान्य करें
    select(person_id, first_name, last_name, test_date, score) %>%
    mutate(test_date = mdy(test_date)) %>%
    filter(!is.na(score))
}
R में फ़ंक्शन लिखने का परिचय

अंतिम असाइनमेंट हटाएँ

import_test_scores <- function(filename) {
  test_scores_raw <- read_csv(filename)

  test_scores_raw %>%  # <- असाइनमेंट हटाएँ
    select(person_id, first_name, last_name, test_date, score) %>%
    mutate(test_date = mdy(test_date)) %>%
    filter(!is.na(score))
}
R में फ़ंक्शन लिखने का परिचय

अपना फंक्शन उपयोग करें

test_scores_geography <- import_test_scores("test_scores_geography.csv")
test_scores_english <- import_test_scores("test_scores_english.csv")
test_scores_art <- import_test_scores("test_scores_art.csv")
test_scores_spanish <- import_test_scores("test_scores_spanish.csv")
R में फ़ंक्शन लिखने का परिचय

sample() के आर्ग्युमेंट्स

  • x: जिन मानों से सैंपल लेना है, उनका वेक्टर.
  • size: x से कितनी बार सैंपल लेना है?
  • replace: रिप्लेसमेंट के साथ सैंपल लें या नहीं?
  • prob: x के हर मान के लिए सैंपलिंग वेट्स का वेक्टर, जिनका योग 1 हो.
R में फ़ंक्शन लिखने का परिचय

अभ्यास करते हैं!

R में फ़ंक्शन लिखने का परिचय

Preparing Video For Download...