Hướng dẫn tidyr cheatsheet - cheatsheet gọn gàng

Tổng quan

Giảng dạy: 50 phút Bài tập: 30 phút 50 min
Exercises: 30 min

Show

Câu hỏi

  • Làm thế nào tôi có thể chọn các hàng và/hoặc cột cụ thể từ DataFrame?

  • Làm thế nào tôi có thể kết hợp nhiều lệnh thành một lệnh?

  • Làm cách nào tôi có thể tạo các cột mới hoặc xóa các cột hiện có khỏi DataFrame?

  • Làm thế nào tôi có thể định dạng lại một khung dữ liệu để đáp ứng nhu cầu của tôi?

Mục tiêu

  • Mô tả mục đích của gói R và các gói

    # A tibble: 10 × 14
       key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                           
     1     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
     2     49 Chirodzo 2016-11-16 00:00:00         6        26 burntb…     2    
     3     52 Chirodzo 2016-11-16 00:00:00        11        15 burntb…     3 no     
     4     56 Chirodzo 2016-11-16 00:00:00        12        23 burntb…     2 yes    
     5     65 Chirodzo 2016-11-16 00:00:00         8        20 burntb…     3 no     
     6     66 Chirodzo 2016-11-16 00:00:00        10        37 burntb…     3 yes    
     7     67 Chirodzo 2016-11-16 00:00:00         5        31 burntb…     2 no     
     8     68 Chirodzo 2016-11-16 00:00:00         8        52 burntb…     3 no     
     9    199 Chirodzo 2017-06-04 00:00:00         7        17 burntb…     2 yes    
    10    200 Chirodzo 2017-06-04 00:00:00         8        20 burntb…     2    
    # … with 6 more variables: affect_conflicts , liv_count ,
    #   items_owned , no_meals , months_lack_food ,
    #   instanceID , and abbreviated variable names ¹​respondent_wall_type,
    #   ²​memb_assoc
    
    9 và
    # filters observations with "|" logical operator
    # output dataframe satisfies AT LEAST ONE of the specified conditions
    filter(interviews, village == "Chirodzo" | village == "Ruaca")
    
    0.
    # A tibble: 10 × 14
       key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                           
     1     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
     2     49 Chirodzo 2016-11-16 00:00:00         6        26 burntb…     2    
     3     52 Chirodzo 2016-11-16 00:00:00        11        15 burntb…     3 no     
     4     56 Chirodzo 2016-11-16 00:00:00        12        23 burntb…     2 yes    
     5     65 Chirodzo 2016-11-16 00:00:00         8        20 burntb…     3 no     
     6     66 Chirodzo 2016-11-16 00:00:00        10        37 burntb…     3 yes    
     7     67 Chirodzo 2016-11-16 00:00:00         5        31 burntb…     2 no     
     8     68 Chirodzo 2016-11-16 00:00:00         8        52 burntb…     3 no     
     9    199 Chirodzo 2017-06-04 00:00:00         7        17 burntb…     2 yes    
    10    200 Chirodzo 2017-06-04 00:00:00         8        20 burntb…     2    
    # … with 6 more variables: affect_conflicts , liv_count ,
    #   items_owned , no_meals , months_lack_food ,
    #   instanceID , and abbreviated variable names ¹​respondent_wall_type,
    #   ²​memb_assoc
    
    9
    and
    # filters observations with "|" logical operator
    # output dataframe satisfies AT LEAST ONE of the specified conditions
    filter(interviews, village == "Chirodzo" | village == "Ruaca")
    
    0
    packages.

  • Chọn một số cột nhất định trong một khung dữ liệu với hàm

    # A tibble: 10 × 14
       key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                           
     1     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
     2     49 Chirodzo 2016-11-16 00:00:00         6        26 burntb…     2    
     3     52 Chirodzo 2016-11-16 00:00:00        11        15 burntb…     3 no     
     4     56 Chirodzo 2016-11-16 00:00:00        12        23 burntb…     2 yes    
     5     65 Chirodzo 2016-11-16 00:00:00         8        20 burntb…     3 no     
     6     66 Chirodzo 2016-11-16 00:00:00        10        37 burntb…     3 yes    
     7     67 Chirodzo 2016-11-16 00:00:00         5        31 burntb…     2 no     
     8     68 Chirodzo 2016-11-16 00:00:00         8        52 burntb…     3 no     
     9    199 Chirodzo 2017-06-04 00:00:00         7        17 burntb…     2 yes    
    10    200 Chirodzo 2017-06-04 00:00:00         8        20 burntb…     2    
    # … with 6 more variables: affect_conflicts , liv_count ,
    #   items_owned , no_meals , months_lack_food ,
    #   instanceID , and abbreviated variable names ¹​respondent_wall_type,
    #   ²​memb_assoc
    
    9
    # filters observations with "|" logical operator
    # output dataframe satisfies AT LEAST ONE of the specified conditions
    filter(interviews, village == "Chirodzo" | village == "Ruaca")
    
    2.
    # A tibble: 10 × 14
       key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                           
     1     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
     2     49 Chirodzo 2016-11-16 00:00:00         6        26 burntb…     2    
     3     52 Chirodzo 2016-11-16 00:00:00        11        15 burntb…     3 no     
     4     56 Chirodzo 2016-11-16 00:00:00        12        23 burntb…     2 yes    
     5     65 Chirodzo 2016-11-16 00:00:00         8        20 burntb…     3 no     
     6     66 Chirodzo 2016-11-16 00:00:00        10        37 burntb…     3 yes    
     7     67 Chirodzo 2016-11-16 00:00:00         5        31 burntb…     2 no     
     8     68 Chirodzo 2016-11-16 00:00:00         8        52 burntb…     3 no     
     9    199 Chirodzo 2017-06-04 00:00:00         7        17 burntb…     2 yes    
    10    200 Chirodzo 2017-06-04 00:00:00         8        20 burntb…     2    
    # … with 6 more variables: affect_conflicts , liv_count ,
    #   items_owned , no_meals , months_lack_food ,
    #   instanceID , and abbreviated variable names ¹​respondent_wall_type,
    #   ²​memb_assoc
    
    9
    function
    # filters observations with "|" logical operator
    # output dataframe satisfies AT LEAST ONE of the specified conditions
    filter(interviews, village == "Chirodzo" | village == "Ruaca")
    
    2.

  • Chọn một số hàng nhất định trong khung dữ liệu theo điều kiện lọc với hàm

    # A tibble: 10 × 14
       key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                           
     1     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
     2     49 Chirodzo 2016-11-16 00:00:00         6        26 burntb…     2    
     3     52 Chirodzo 2016-11-16 00:00:00        11        15 burntb…     3 no     
     4     56 Chirodzo 2016-11-16 00:00:00        12        23 burntb…     2 yes    
     5     65 Chirodzo 2016-11-16 00:00:00         8        20 burntb…     3 no     
     6     66 Chirodzo 2016-11-16 00:00:00        10        37 burntb…     3 yes    
     7     67 Chirodzo 2016-11-16 00:00:00         5        31 burntb…     2 no     
     8     68 Chirodzo 2016-11-16 00:00:00         8        52 burntb…     3 no     
     9    199 Chirodzo 2017-06-04 00:00:00         7        17 burntb…     2 yes    
    10    200 Chirodzo 2017-06-04 00:00:00         8        20 burntb…     2    
    # … with 6 more variables: affect_conflicts , liv_count ,
    #   items_owned , no_meals , months_lack_food ,
    #   instanceID , and abbreviated variable names ¹​respondent_wall_type,
    #   ²​memb_assoc
    
    9
    # filters observations with "|" logical operator
    # output dataframe satisfies AT LEAST ONE of the specified conditions
    filter(interviews, village == "Chirodzo" | village == "Ruaca")
    
    4.
    # A tibble: 10 × 14
       key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                           
     1     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
     2     49 Chirodzo 2016-11-16 00:00:00         6        26 burntb…     2    
     3     52 Chirodzo 2016-11-16 00:00:00        11        15 burntb…     3 no     
     4     56 Chirodzo 2016-11-16 00:00:00        12        23 burntb…     2 yes    
     5     65 Chirodzo 2016-11-16 00:00:00         8        20 burntb…     3 no     
     6     66 Chirodzo 2016-11-16 00:00:00        10        37 burntb…     3 yes    
     7     67 Chirodzo 2016-11-16 00:00:00         5        31 burntb…     2 no     
     8     68 Chirodzo 2016-11-16 00:00:00         8        52 burntb…     3 no     
     9    199 Chirodzo 2017-06-04 00:00:00         7        17 burntb…     2 yes    
    10    200 Chirodzo 2017-06-04 00:00:00         8        20 burntb…     2    
    # … with 6 more variables: affect_conflicts , liv_count ,
    #   items_owned , no_meals , months_lack_food ,
    #   instanceID , and abbreviated variable names ¹​respondent_wall_type,
    #   ²​memb_assoc
    
    9
    function
    # filters observations with "|" logical operator
    # output dataframe satisfies AT LEAST ONE of the specified conditions
    filter(interviews, village == "Chirodzo" | village == "Ruaca")
    
    4.

  • Liên kết đầu ra của một hàm

    # A tibble: 10 × 14
       key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                           
     1     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
     2     49 Chirodzo 2016-11-16 00:00:00         6        26 burntb…     2    
     3     52 Chirodzo 2016-11-16 00:00:00        11        15 burntb…     3 no     
     4     56 Chirodzo 2016-11-16 00:00:00        12        23 burntb…     2 yes    
     5     65 Chirodzo 2016-11-16 00:00:00         8        20 burntb…     3 no     
     6     66 Chirodzo 2016-11-16 00:00:00        10        37 burntb…     3 yes    
     7     67 Chirodzo 2016-11-16 00:00:00         5        31 burntb…     2 no     
     8     68 Chirodzo 2016-11-16 00:00:00         8        52 burntb…     3 no     
     9    199 Chirodzo 2017-06-04 00:00:00         7        17 burntb…     2 yes    
    10    200 Chirodzo 2017-06-04 00:00:00         8        20 burntb…     2    
    # … with 6 more variables: affect_conflicts , liv_count ,
    #   items_owned , no_meals , months_lack_food ,
    #   instanceID , and abbreviated variable names ¹​respondent_wall_type,
    #   ²​memb_assoc
    
    9 với đầu vào của một hàm khác với toán tử ____
    # filters observations with "|" logical operator
    # output dataframe satisfies AT LEAST ONE of the specified conditions
    filter(interviews, village == "Chirodzo" | village == "Ruaca")
    
    6.
    # A tibble: 10 × 14
       key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                           
     1     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
     2     49 Chirodzo 2016-11-16 00:00:00         6        26 burntb…     2    
     3     52 Chirodzo 2016-11-16 00:00:00        11        15 burntb…     3 no     
     4     56 Chirodzo 2016-11-16 00:00:00        12        23 burntb…     2 yes    
     5     65 Chirodzo 2016-11-16 00:00:00         8        20 burntb…     3 no     
     6     66 Chirodzo 2016-11-16 00:00:00        10        37 burntb…     3 yes    
     7     67 Chirodzo 2016-11-16 00:00:00         5        31 burntb…     2 no     
     8     68 Chirodzo 2016-11-16 00:00:00         8        52 burntb…     3 no     
     9    199 Chirodzo 2017-06-04 00:00:00         7        17 burntb…     2 yes    
    10    200 Chirodzo 2017-06-04 00:00:00         8        20 burntb…     2    
    # … with 6 more variables: affect_conflicts , liv_count ,
    #   items_owned , no_meals , months_lack_food ,
    #   instanceID , and abbreviated variable names ¹​respondent_wall_type,
    #   ²​memb_assoc
    
    9
    function to the input of another function with the ‘pipe’ operator
    # filters observations with "|" logical operator
    # output dataframe satisfies AT LEAST ONE of the specified conditions
    filter(interviews, village == "Chirodzo" | village == "Ruaca")
    
    6.

  • Thêm các cột mới vào một khung dữ liệu là chức năng của các cột hiện có với

    # filters observations with "|" logical operator
    # output dataframe satisfies AT LEAST ONE of the specified conditions
    filter(interviews, village == "Chirodzo" | village == "Ruaca")
    
    7.

  • Sử dụng khái niệm phân chia-apply-bombine để phân tích dữ liệu.

  • Sử dụng

    # filters observations with "|" logical operator
    # output dataframe satisfies AT LEAST ONE of the specified conditions
    filter(interviews, village == "Chirodzo" | village == "Ruaca")
    
    8,
    # filters observations with "|" logical operator
    # output dataframe satisfies AT LEAST ONE of the specified conditions
    filter(interviews, village == "Chirodzo" | village == "Ruaca")
    
    9 và
    # A tibble: 88 × 14
       key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                           
     1      8 Chirodzo 2016-11-16 00:00:00        12        70 burntb…     3 yes    
     2      9 Chirodzo 2016-11-16 00:00:00         8         6 burntb…     1 no     
     3     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
     4     23 Ruaca    2016-11-21 00:00:00        10        20 burntb…     4    
     5     24 Ruaca    2016-11-21 00:00:00         6         4 burntb…     2 no     
     6     25 Ruaca    2016-11-21 00:00:00        11         6 burntb…     3 no     
     7     26 Ruaca    2016-11-21 00:00:00         3        20 burntb…     2 no     
     8     27 Ruaca    2016-11-21 00:00:00         7        36 burntb…     2    
     9     28 Ruaca    2016-11-21 00:00:00         2         2 muddaub     1 no     
    10     29 Ruaca    2016-11-21 00:00:00         7        10 burntb…     2 yes    
    # … with 78 more rows, 6 more variables: affect_conflicts ,
    #   liv_count , items_owned , no_meals , months_lack_food ,
    #   instanceID , and abbreviated variable names ¹​respondent_wall_type,
    #   ²​memb_assoc
    
    0 để chia một khung dữ liệu thành các nhóm quan sát, áp dụng một số liệu thống kê tóm tắt cho mỗi nhóm, sau đó kết hợp các kết quả.

  • Mô tả khái niệm về một định dạng bảng rộng và dài và với mục đích đó các định dạng đó là hữu ích.

  • Mô tả vai trò của các tên biến và các giá trị liên quan của chúng khi một bảng được định hình lại.

  • Định hình lại một khung dữ liệu từ dài đến định dạng rộng và quay lại với các lệnh

    # A tibble: 88 × 14
       key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                           
     1      8 Chirodzo 2016-11-16 00:00:00        12        70 burntb…     3 yes    
     2      9 Chirodzo 2016-11-16 00:00:00         8         6 burntb…     1 no     
     3     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
     4     23 Ruaca    2016-11-21 00:00:00        10        20 burntb…     4    
     5     24 Ruaca    2016-11-21 00:00:00         6         4 burntb…     2 no     
     6     25 Ruaca    2016-11-21 00:00:00        11         6 burntb…     3 no     
     7     26 Ruaca    2016-11-21 00:00:00         3        20 burntb…     2 no     
     8     27 Ruaca    2016-11-21 00:00:00         7        36 burntb…     2    
     9     28 Ruaca    2016-11-21 00:00:00         2         2 muddaub     1 no     
    10     29 Ruaca    2016-11-21 00:00:00         7        10 burntb…     2 yes    
    # … with 78 more rows, 6 more variables: affect_conflicts ,
    #   liv_count , items_owned , no_meals , months_lack_food ,
    #   instanceID , and abbreviated variable names ¹​respondent_wall_type,
    #   ²​memb_assoc
    
    1 và
    # A tibble: 88 × 14
       key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                           
     1      8 Chirodzo 2016-11-16 00:00:00        12        70 burntb…     3 yes    
     2      9 Chirodzo 2016-11-16 00:00:00         8         6 burntb…     1 no     
     3     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
     4     23 Ruaca    2016-11-21 00:00:00        10        20 burntb…     4    
     5     24 Ruaca    2016-11-21 00:00:00         6         4 burntb…     2 no     
     6     25 Ruaca    2016-11-21 00:00:00        11         6 burntb…     3 no     
     7     26 Ruaca    2016-11-21 00:00:00         3        20 burntb…     2 no     
     8     27 Ruaca    2016-11-21 00:00:00         7        36 burntb…     2    
     9     28 Ruaca    2016-11-21 00:00:00         2         2 muddaub     1 no     
    10     29 Ruaca    2016-11-21 00:00:00         7        10 burntb…     2 yes    
    # … with 78 more rows, 6 more variables: affect_conflicts ,
    #   liv_count , items_owned , no_meals , months_lack_food ,
    #   instanceID , and abbreviated variable names ¹​respondent_wall_type,
    #   ²​memb_assoc
    
    2 từ gói
    # filters observations with "|" logical operator
    # output dataframe satisfies AT LEAST ONE of the specified conditions
    filter(interviews, village == "Chirodzo" | village == "Ruaca")
    
    0.
    # filters observations with "|" logical operator
    # output dataframe satisfies AT LEAST ONE of the specified conditions
    filter(interviews, village == "Chirodzo" | village == "Ruaca")
    
    0
    package.

  • Xuất một dữ liệu vào tệp CSV.

# A tibble: 10 × 14
   key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                       
 1     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
 2     49 Chirodzo 2016-11-16 00:00:00         6        26 burntb…     2    
 3     52 Chirodzo 2016-11-16 00:00:00        11        15 burntb…     3 no     
 4     56 Chirodzo 2016-11-16 00:00:00        12        23 burntb…     2 yes    
 5     65 Chirodzo 2016-11-16 00:00:00         8        20 burntb…     3 no     
 6     66 Chirodzo 2016-11-16 00:00:00        10        37 burntb…     3 yes    
 7     67 Chirodzo 2016-11-16 00:00:00         5        31 burntb…     2 no     
 8     68 Chirodzo 2016-11-16 00:00:00         8        52 burntb…     3 no     
 9    199 Chirodzo 2017-06-04 00:00:00         7        17 burntb…     2 yes    
10    200 Chirodzo 2017-06-04 00:00:00         8        20 burntb…     2    
# … with 6 more variables: affect_conflicts , liv_count ,
#   items_owned , no_meals , months_lack_food ,
#   instanceID , and abbreviated variable names ¹​respondent_wall_type,
#   ²​memb_assoc
9 là một gói để làm cho dữ liệu bảng tìm kiếm dễ dàng hơn bằng cách sử dụng một bộ chức năng giới hạn có thể được kết hợp để trích xuất và tóm tắt những hiểu biết từ dữ liệu của bạn. Nó kết hợp độc đáo với
# filters observations with "|" logical operator
# output dataframe satisfies AT LEAST ONE of the specified conditions
filter(interviews, village == "Chirodzo" | village == "Ruaca")
0 cho phép bạn nhanh chóng chuyển đổi giữa các định dạng dữ liệu khác nhau (dài so với rộng) để vẽ và phân tích.
is a package for making tabular data wrangling easier by using a limited set of functions that can be combined to extract and summarize insights from your data. It pairs nicely with
# filters observations with "|" logical operator
# output dataframe satisfies AT LEAST ONE of the specified conditions
filter(interviews, village == "Chirodzo" | village == "Ruaca")
0
which enables you to swiftly convert between different data formats (long vs. wide) for plotting and analysis.

Tương tự như

# A tibble: 88 × 14
   key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                       
 1      8 Chirodzo 2016-11-16 00:00:00        12        70 burntb…     3 yes    
 2      9 Chirodzo 2016-11-16 00:00:00         8         6 burntb…     1 no     
 3     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
 4     23 Ruaca    2016-11-21 00:00:00        10        20 burntb…     4    
 5     24 Ruaca    2016-11-21 00:00:00         6         4 burntb…     2 no     
 6     25 Ruaca    2016-11-21 00:00:00        11         6 burntb…     3 no     
 7     26 Ruaca    2016-11-21 00:00:00         3        20 burntb…     2 no     
 8     27 Ruaca    2016-11-21 00:00:00         7        36 burntb…     2    
 9     28 Ruaca    2016-11-21 00:00:00         2         2 muddaub     1 no     
10     29 Ruaca    2016-11-21 00:00:00         7        10 burntb…     2 yes    
# … with 78 more rows, 6 more variables: affect_conflicts ,
#   liv_count , items_owned , no_meals , months_lack_food ,
#   instanceID , and abbreviated variable names ¹​respondent_wall_type,
#   ²​memb_assoc
6,
# A tibble: 10 × 14
   key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                       
 1     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
 2     49 Chirodzo 2016-11-16 00:00:00         6        26 burntb…     2    
 3     52 Chirodzo 2016-11-16 00:00:00        11        15 burntb…     3 no     
 4     56 Chirodzo 2016-11-16 00:00:00        12        23 burntb…     2 yes    
 5     65 Chirodzo 2016-11-16 00:00:00         8        20 burntb…     3 no     
 6     66 Chirodzo 2016-11-16 00:00:00        10        37 burntb…     3 yes    
 7     67 Chirodzo 2016-11-16 00:00:00         5        31 burntb…     2 no     
 8     68 Chirodzo 2016-11-16 00:00:00         8        52 burntb…     3 no     
 9    199 Chirodzo 2017-06-04 00:00:00         7        17 burntb…     2 yes    
10    200 Chirodzo 2017-06-04 00:00:00         8        20 burntb…     2    
# … with 6 more variables: affect_conflicts , liv_count ,
#   items_owned , no_meals , months_lack_food ,
#   instanceID , and abbreviated variable names ¹​respondent_wall_type,
#   ²​memb_assoc
9 và
# filters observations with "|" logical operator
# output dataframe satisfies AT LEAST ONE of the specified conditions
filter(interviews, village == "Chirodzo" | village == "Ruaca")
0 cũng là một phần của Tidyverse. Các gói này đã được tải trong bộ nhớ R, khi chúng tôi gọi
# A tibble: 88 × 14
   key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                       
 1      8 Chirodzo 2016-11-16 00:00:00        12        70 burntb…     3 yes    
 2      9 Chirodzo 2016-11-16 00:00:00         8         6 burntb…     1 no     
 3     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
 4     23 Ruaca    2016-11-21 00:00:00        10        20 burntb…     4    
 5     24 Ruaca    2016-11-21 00:00:00         6         4 burntb…     2 no     
 6     25 Ruaca    2016-11-21 00:00:00        11         6 burntb…     3 no     
 7     26 Ruaca    2016-11-21 00:00:00         3        20 burntb…     2 no     
 8     27 Ruaca    2016-11-21 00:00:00         7        36 burntb…     2    
 9     28 Ruaca    2016-11-21 00:00:00         2         2 muddaub     1 no     
10     29 Ruaca    2016-11-21 00:00:00         7        10 burntb…     2 yes    
# … with 78 more rows, 6 more variables: affect_conflicts ,
#   liv_count , items_owned , no_meals , months_lack_food ,
#   instanceID , and abbreviated variable names ¹​respondent_wall_type,
#   ²​memb_assoc
9 trước đó.
# A tibble: 88 × 14
   key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                       
 1      8 Chirodzo 2016-11-16 00:00:00        12        70 burntb…     3 yes    
 2      9 Chirodzo 2016-11-16 00:00:00         8         6 burntb…     1 no     
 3     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
 4     23 Ruaca    2016-11-21 00:00:00        10        20 burntb…     4    
 5     24 Ruaca    2016-11-21 00:00:00         6         4 burntb…     2 no     
 6     25 Ruaca    2016-11-21 00:00:00        11         6 burntb…     3 no     
 7     26 Ruaca    2016-11-21 00:00:00         3        20 burntb…     2 no     
 8     27 Ruaca    2016-11-21 00:00:00         7        36 burntb…     2    
 9     28 Ruaca    2016-11-21 00:00:00         2         2 muddaub     1 no     
10     29 Ruaca    2016-11-21 00:00:00         7        10 burntb…     2 yes    
# … with 78 more rows, 6 more variables: affect_conflicts ,
#   liv_count , items_owned , no_meals , months_lack_food ,
#   instanceID , and abbreviated variable names ¹​respondent_wall_type,
#   ²​memb_assoc
6
,
# A tibble: 10 × 14
   key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                       
 1     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
 2     49 Chirodzo 2016-11-16 00:00:00         6        26 burntb…     2    
 3     52 Chirodzo 2016-11-16 00:00:00        11        15 burntb…     3 no     
 4     56 Chirodzo 2016-11-16 00:00:00        12        23 burntb…     2 yes    
 5     65 Chirodzo 2016-11-16 00:00:00         8        20 burntb…     3 no     
 6     66 Chirodzo 2016-11-16 00:00:00        10        37 burntb…     3 yes    
 7     67 Chirodzo 2016-11-16 00:00:00         5        31 burntb…     2 no     
 8     68 Chirodzo 2016-11-16 00:00:00         8        52 burntb…     3 no     
 9    199 Chirodzo 2017-06-04 00:00:00         7        17 burntb…     2 yes    
10    200 Chirodzo 2017-06-04 00:00:00         8        20 burntb…     2    
# … with 6 more variables: affect_conflicts , liv_count ,
#   items_owned , no_meals , months_lack_food ,
#   instanceID , and abbreviated variable names ¹​respondent_wall_type,
#   ²​memb_assoc
9
and
# filters observations with "|" logical operator
# output dataframe satisfies AT LEAST ONE of the specified conditions
filter(interviews, village == "Chirodzo" | village == "Ruaca")
0
are also part of the tidyverse. These packages were loaded in R’s memory when we called
# A tibble: 88 × 14
   key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                       
 1      8 Chirodzo 2016-11-16 00:00:00        12        70 burntb…     3 yes    
 2      9 Chirodzo 2016-11-16 00:00:00         8         6 burntb…     1 no     
 3     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
 4     23 Ruaca    2016-11-21 00:00:00        10        20 burntb…     4    
 5     24 Ruaca    2016-11-21 00:00:00         6         4 burntb…     2 no     
 6     25 Ruaca    2016-11-21 00:00:00        11         6 burntb…     3 no     
 7     26 Ruaca    2016-11-21 00:00:00         3        20 burntb…     2 no     
 8     27 Ruaca    2016-11-21 00:00:00         7        36 burntb…     2    
 9     28 Ruaca    2016-11-21 00:00:00         2         2 muddaub     1 no     
10     29 Ruaca    2016-11-21 00:00:00         7        10 burntb…     2 yes    
# … with 78 more rows, 6 more variables: affect_conflicts ,
#   liv_count , items_owned , no_meals , months_lack_food ,
#   instanceID , and abbreviated variable names ¹​respondent_wall_type,
#   ²​memb_assoc
9 earlier.

Ghi chú

Các gói trong Tidyverse, cụ thể là

# A tibble: 10 × 14
   key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                       
 1     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
 2     49 Chirodzo 2016-11-16 00:00:00         6        26 burntb…     2    
 3     52 Chirodzo 2016-11-16 00:00:00        11        15 burntb…     3 no     
 4     56 Chirodzo 2016-11-16 00:00:00        12        23 burntb…     2 yes    
 5     65 Chirodzo 2016-11-16 00:00:00         8        20 burntb…     3 no     
 6     66 Chirodzo 2016-11-16 00:00:00        10        37 burntb…     3 yes    
 7     67 Chirodzo 2016-11-16 00:00:00         5        31 burntb…     2 no     
 8     68 Chirodzo 2016-11-16 00:00:00         8        52 burntb…     3 no     
 9    199 Chirodzo 2017-06-04 00:00:00         7        17 burntb…     2 yes    
10    200 Chirodzo 2017-06-04 00:00:00         8        20 burntb…     2    
# … with 6 more variables: affect_conflicts , liv_count ,
#   items_owned , no_meals , months_lack_food ,
#   instanceID , and abbreviated variable names ¹​respondent_wall_type,
#   ²​memb_assoc
9,
# filters observations with "|" logical operator
# output dataframe satisfies AT LEAST ONE of the specified conditions
filter(interviews, village == "Chirodzo" | village == "Ruaca")
0 và
# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
02 chấp nhận cả người Anh (ví dụ: tóm tắt) và các biến thể chính tả của Mỹ (ví dụ: tóm tắt) của các tên chức năng và tùy chọn khác nhau. Đối với bài học này, chúng tôi sử dụng các cách viết của Mỹ về các chức năng khác nhau; Tuy nhiên, hãy thoải mái sử dụng biến thể khu vực cho nơi bạn đang dạy.
# A tibble: 10 × 14
   key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                       
 1     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
 2     49 Chirodzo 2016-11-16 00:00:00         6        26 burntb…     2    
 3     52 Chirodzo 2016-11-16 00:00:00        11        15 burntb…     3 no     
 4     56 Chirodzo 2016-11-16 00:00:00        12        23 burntb…     2 yes    
 5     65 Chirodzo 2016-11-16 00:00:00         8        20 burntb…     3 no     
 6     66 Chirodzo 2016-11-16 00:00:00        10        37 burntb…     3 yes    
 7     67 Chirodzo 2016-11-16 00:00:00         5        31 burntb…     2 no     
 8     68 Chirodzo 2016-11-16 00:00:00         8        52 burntb…     3 no     
 9    199 Chirodzo 2017-06-04 00:00:00         7        17 burntb…     2 yes    
10    200 Chirodzo 2017-06-04 00:00:00         8        20 burntb…     2    
# … with 6 more variables: affect_conflicts , liv_count ,
#   items_owned , no_meals , months_lack_food ,
#   instanceID , and abbreviated variable names ¹​respondent_wall_type,
#   ²​memb_assoc
9
,
# filters observations with "|" logical operator
# output dataframe satisfies AT LEAST ONE of the specified conditions
filter(interviews, village == "Chirodzo" | village == "Ruaca")
0
and
# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
02
accept both the British (e.g. summarise) and American (e.g. summarize) spelling variants of different function and option names. For this lesson, we utilize the American spellings of different functions; however, feel free to use the regional variant for where you are teaching.

Gói R là gì?

Gói

# A tibble: 10 × 14
   key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                       
 1     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
 2     49 Chirodzo 2016-11-16 00:00:00         6        26 burntb…     2    
 3     52 Chirodzo 2016-11-16 00:00:00        11        15 burntb…     3 no     
 4     56 Chirodzo 2016-11-16 00:00:00        12        23 burntb…     2 yes    
 5     65 Chirodzo 2016-11-16 00:00:00         8        20 burntb…     3 no     
 6     66 Chirodzo 2016-11-16 00:00:00        10        37 burntb…     3 yes    
 7     67 Chirodzo 2016-11-16 00:00:00         5        31 burntb…     2 no     
 8     68 Chirodzo 2016-11-16 00:00:00         8        52 burntb…     3 no     
 9    199 Chirodzo 2017-06-04 00:00:00         7        17 burntb…     2 yes    
10    200 Chirodzo 2017-06-04 00:00:00         8        20 burntb…     2    
# … with 6 more variables: affect_conflicts , liv_count ,
#   items_owned , no_meals , months_lack_food ,
#   instanceID , and abbreviated variable names ¹​respondent_wall_type,
#   ²​memb_assoc
9 cung cấp các công cụ dễ dàng cho các tác vụ gây tranh cãi dữ liệu phổ biến nhất. Nó được xây dựng để làm việc trực tiếp với các khung dữ liệu, với nhiều tác vụ phổ biến được tối ưu hóa bằng cách được viết bằng ngôn ngữ được biên dịch (C ++) (không phải tất cả các gói R đều được viết bằng R!).
# A tibble: 10 × 14
   key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                       
 1     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
 2     49 Chirodzo 2016-11-16 00:00:00         6        26 burntb…     2    
 3     52 Chirodzo 2016-11-16 00:00:00        11        15 burntb…     3 no     
 4     56 Chirodzo 2016-11-16 00:00:00        12        23 burntb…     2 yes    
 5     65 Chirodzo 2016-11-16 00:00:00         8        20 burntb…     3 no     
 6     66 Chirodzo 2016-11-16 00:00:00        10        37 burntb…     3 yes    
 7     67 Chirodzo 2016-11-16 00:00:00         5        31 burntb…     2 no     
 8     68 Chirodzo 2016-11-16 00:00:00         8        52 burntb…     3 no     
 9    199 Chirodzo 2017-06-04 00:00:00         7        17 burntb…     2 yes    
10    200 Chirodzo 2017-06-04 00:00:00         8        20 burntb…     2    
# … with 6 more variables: affect_conflicts , liv_count ,
#   items_owned , no_meals , months_lack_food ,
#   instanceID , and abbreviated variable names ¹​respondent_wall_type,
#   ²​memb_assoc
9
provides easy tools for the most common data wrangling tasks. It is built to work directly with dataframes, with many common tasks optimized by being written in a compiled language (C++) (not all R packages are written in R!).

Gói

# filters observations with "|" logical operator
# output dataframe satisfies AT LEAST ONE of the specified conditions
filter(interviews, village == "Chirodzo" | village == "Ruaca")
0 giải quyết vấn đề chung là muốn định hình lại dữ liệu của bạn để vẽ và sử dụng bởi các chức năng R khác nhau. Đôi khi chúng tôi muốn các bộ dữ liệu nơi chúng tôi có một hàng cho mỗi lần đo. Đôi khi chúng tôi muốn một khung dữ liệu trong đó mỗi loại đo có cột riêng và các hàng thay vào đó là các nhóm tổng hợp hơn. Di chuyển qua lại giữa các định dạng này là không cần thiết và
# filters observations with "|" logical operator
# output dataframe satisfies AT LEAST ONE of the specified conditions
filter(interviews, village == "Chirodzo" | village == "Ruaca")
0 cung cấp cho bạn các công cụ cho việc gây tranh cãi dữ liệu này và tinh vi hơn.
# filters observations with "|" logical operator
# output dataframe satisfies AT LEAST ONE of the specified conditions
filter(interviews, village == "Chirodzo" | village == "Ruaca")
0
addresses the common problem of wanting to reshape your data for plotting and use by different R functions. Sometimes we want data sets where we have one row per measurement. Sometimes we want a dataframe where each measurement type has its own column, and rows are instead more aggregated groups. Moving back and forth between these formats is nontrivial, and
# filters observations with "|" logical operator
# output dataframe satisfies AT LEAST ONE of the specified conditions
filter(interviews, village == "Chirodzo" | village == "Ruaca")
0
gives you tools for this and more sophisticated data wrangling.

Nhưng cũng có các gói có sẵn cho một loạt các tác vụ bao gồm các lô xây dựng (____10102, mà chúng tôi sẽ thấy sau), tải xuống dữ liệu từ cơ sở dữ liệu NCBI hoặc thực hiện phân tích thống kê trên bộ dữ liệu của bạn. Nhiều gói như thế này được lưu trữ và có thể tải xuống từ Mạng Rarchive toàn diện (CRAN) bằng cách sử dụng

# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
07. Hàm này làm cho gói có thể truy cập bằng cách cài đặt R của bạn với lệnh
# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
08, như bạn đã làm với
# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
09 trước đó.
# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
02
, which we’ll see later), downloading data from the NCBI database, or performing statistical analysis on your data set. Many packages such as these are housed on, and downloadable from, the Comprehensive R Archive Network (CRAN) using
# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
07. This function makes the package accessible by your R installation with the command
# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
08, as you did with
# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
09 earlier.

Để dễ dàng truy cập tài liệu cho một gói trong r hoặc rstudio, hãy sử dụng

# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
10.

Để tìm hiểu thêm về

# A tibble: 10 × 14
   key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                       
 1     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
 2     49 Chirodzo 2016-11-16 00:00:00         6        26 burntb…     2    
 3     52 Chirodzo 2016-11-16 00:00:00        11        15 burntb…     3 no     
 4     56 Chirodzo 2016-11-16 00:00:00        12        23 burntb…     2 yes    
 5     65 Chirodzo 2016-11-16 00:00:00         8        20 burntb…     3 no     
 6     66 Chirodzo 2016-11-16 00:00:00        10        37 burntb…     3 yes    
 7     67 Chirodzo 2016-11-16 00:00:00         5        31 burntb…     2 no     
 8     68 Chirodzo 2016-11-16 00:00:00         8        52 burntb…     3 no     
 9    199 Chirodzo 2017-06-04 00:00:00         7        17 burntb…     2 yes    
10    200 Chirodzo 2017-06-04 00:00:00         8        20 burntb…     2    
# … with 6 more variables: affect_conflicts , liv_count ,
#   items_owned , no_meals , months_lack_food ,
#   instanceID , and abbreviated variable names ¹​respondent_wall_type,
#   ²​memb_assoc
9 và
# filters observations with "|" logical operator
# output dataframe satisfies AT LEAST ONE of the specified conditions
filter(interviews, village == "Chirodzo" | village == "Ruaca")
0 sau hội thảo, bạn có thể muốn xem chuyển đổi dữ liệu tiện dụng này với
# A tibble: 10 × 14
   key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                       
 1     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
 2     49 Chirodzo 2016-11-16 00:00:00         6        26 burntb…     2    
 3     52 Chirodzo 2016-11-16 00:00:00        11        15 burntb…     3 no     
 4     56 Chirodzo 2016-11-16 00:00:00        12        23 burntb…     2 yes    
 5     65 Chirodzo 2016-11-16 00:00:00         8        20 burntb…     3 no     
 6     66 Chirodzo 2016-11-16 00:00:00        10        37 burntb…     3 yes    
 7     67 Chirodzo 2016-11-16 00:00:00         5        31 burntb…     2 no     
 8     68 Chirodzo 2016-11-16 00:00:00         8        52 burntb…     3 no     
 9    199 Chirodzo 2017-06-04 00:00:00         7        17 burntb…     2 yes    
10    200 Chirodzo 2017-06-04 00:00:00         8        20 burntb…     2    
# … with 6 more variables: affect_conflicts , liv_count ,
#   items_owned , no_meals , months_lack_food ,
#   instanceID , and abbreviated variable names ¹​respondent_wall_type,
#   ²​memb_assoc
9 CheatSheet và cái này về
# filters observations with "|" logical operator
# output dataframe satisfies AT LEAST ONE of the specified conditions
filter(interviews, village == "Chirodzo" | village == "Ruaca")
0.
# A tibble: 10 × 14
   key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                       
 1     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
 2     49 Chirodzo 2016-11-16 00:00:00         6        26 burntb…     2    
 3     52 Chirodzo 2016-11-16 00:00:00        11        15 burntb…     3 no     
 4     56 Chirodzo 2016-11-16 00:00:00        12        23 burntb…     2 yes    
 5     65 Chirodzo 2016-11-16 00:00:00         8        20 burntb…     3 no     
 6     66 Chirodzo 2016-11-16 00:00:00        10        37 burntb…     3 yes    
 7     67 Chirodzo 2016-11-16 00:00:00         5        31 burntb…     2 no     
 8     68 Chirodzo 2016-11-16 00:00:00         8        52 burntb…     3 no     
 9    199 Chirodzo 2017-06-04 00:00:00         7        17 burntb…     2 yes    
10    200 Chirodzo 2017-06-04 00:00:00         8        20 burntb…     2    
# … with 6 more variables: affect_conflicts , liv_count ,
#   items_owned , no_meals , months_lack_food ,
#   instanceID , and abbreviated variable names ¹​respondent_wall_type,
#   ²​memb_assoc
9
and
# filters observations with "|" logical operator
# output dataframe satisfies AT LEAST ONE of the specified conditions
filter(interviews, village == "Chirodzo" | village == "Ruaca")
0
after the workshop, you may want to check out this handy data transformation with
# A tibble: 10 × 14
   key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                       
 1     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
 2     49 Chirodzo 2016-11-16 00:00:00         6        26 burntb…     2    
 3     52 Chirodzo 2016-11-16 00:00:00        11        15 burntb…     3 no     
 4     56 Chirodzo 2016-11-16 00:00:00        12        23 burntb…     2 yes    
 5     65 Chirodzo 2016-11-16 00:00:00         8        20 burntb…     3 no     
 6     66 Chirodzo 2016-11-16 00:00:00        10        37 burntb…     3 yes    
 7     67 Chirodzo 2016-11-16 00:00:00         5        31 burntb…     2 no     
 8     68 Chirodzo 2016-11-16 00:00:00         8        52 burntb…     3 no     
 9    199 Chirodzo 2017-06-04 00:00:00         7        17 burntb…     2 yes    
10    200 Chirodzo 2017-06-04 00:00:00         8        20 burntb…     2    
# … with 6 more variables: affect_conflicts , liv_count ,
#   items_owned , no_meals , months_lack_food ,
#   instanceID , and abbreviated variable names ¹​respondent_wall_type,
#   ²​memb_assoc
9
cheatsheet and this one about
# filters observations with "|" logical operator
# output dataframe satisfies AT LEAST ONE of the specified conditions
filter(interviews, village == "Chirodzo" | village == "Ruaca")
0
.

Ghi chú

Các gói trong Tidyverse, cụ thể là

# A tibble: 10 × 14
   key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                       
 1     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
 2     49 Chirodzo 2016-11-16 00:00:00         6        26 burntb…     2    
 3     52 Chirodzo 2016-11-16 00:00:00        11        15 burntb…     3 no     
 4     56 Chirodzo 2016-11-16 00:00:00        12        23 burntb…     2 yes    
 5     65 Chirodzo 2016-11-16 00:00:00         8        20 burntb…     3 no     
 6     66 Chirodzo 2016-11-16 00:00:00        10        37 burntb…     3 yes    
 7     67 Chirodzo 2016-11-16 00:00:00         5        31 burntb…     2 no     
 8     68 Chirodzo 2016-11-16 00:00:00         8        52 burntb…     3 no     
 9    199 Chirodzo 2017-06-04 00:00:00         7        17 burntb…     2 yes    
10    200 Chirodzo 2017-06-04 00:00:00         8        20 burntb…     2    
# … with 6 more variables: affect_conflicts , liv_count ,
#   items_owned , no_meals , months_lack_food ,
#   instanceID , and abbreviated variable names ¹​respondent_wall_type,
#   ²​memb_assoc
9,
# filters observations with "|" logical operator
# output dataframe satisfies AT LEAST ONE of the specified conditions
filter(interviews, village == "Chirodzo" | village == "Ruaca")
0 và
# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
02 chấp nhận cả người Anh (ví dụ: tóm tắt) và các biến thể chính tả của Mỹ (ví dụ: tóm tắt) của các tên chức năng và tùy chọn khác nhau. Đối với bài học này, chúng tôi sử dụng các cách viết của Mỹ về các chức năng khác nhau; Tuy nhiên, hãy thoải mái sử dụng biến thể khu vực cho nơi bạn đang dạy.

Gói R là gì?# A tibble: 10 × 14 key_ID village interview_date no_membrs years_liv respo…¹ rooms memb_…² 1 10 Chirodzo 2016-12-16 00:00:00 12 23 burntb… 5 no 2 49 Chirodzo 2016-11-16 00:00:00 6 26 burntb… 2 3 52 Chirodzo 2016-11-16 00:00:00 11 15 burntb… 3 no 4 56 Chirodzo 2016-11-16 00:00:00 12 23 burntb… 2 yes 5 65 Chirodzo 2016-11-16 00:00:00 8 20 burntb… 3 no 6 66 Chirodzo 2016-11-16 00:00:00 10 37 burntb… 3 yes 7 67 Chirodzo 2016-11-16 00:00:00 5 31 burntb… 2 no 8 68 Chirodzo 2016-11-16 00:00:00 8 52 burntb… 3 no 9 199 Chirodzo 2017-06-04 00:00:00 7 17 burntb… 2 yes 10 200 Chirodzo 2017-06-04 00:00:00 8 20 burntb… 2 # … with 6 more variables: affect_conflicts , liv_count , # items_owned , no_meals , months_lack_food , # instanceID , and abbreviated variable names ¹​respondent_wall_type, # ²​memb_assoc 9 and # filters observations with "|" logical operator # output dataframe satisfies AT LEAST ONE of the specified conditions filter(interviews, village == "Chirodzo" | village == "Ruaca") 0

Gói

# A tibble: 10 × 14
   key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                       
 1     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
 2     49 Chirodzo 2016-11-16 00:00:00         6        26 burntb…     2    
 3     52 Chirodzo 2016-11-16 00:00:00        11        15 burntb…     3 no     
 4     56 Chirodzo 2016-11-16 00:00:00        12        23 burntb…     2 yes    
 5     65 Chirodzo 2016-11-16 00:00:00         8        20 burntb…     3 no     
 6     66 Chirodzo 2016-11-16 00:00:00        10        37 burntb…     3 yes    
 7     67 Chirodzo 2016-11-16 00:00:00         5        31 burntb…     2 no     
 8     68 Chirodzo 2016-11-16 00:00:00         8        52 burntb…     3 no     
 9    199 Chirodzo 2017-06-04 00:00:00         7        17 burntb…     2 yes    
10    200 Chirodzo 2017-06-04 00:00:00         8        20 burntb…     2    
# … with 6 more variables: affect_conflicts , liv_count ,
#   items_owned , no_meals , months_lack_food ,
#   instanceID , and abbreviated variable names ¹​respondent_wall_type,
#   ²​memb_assoc
9 cung cấp các công cụ dễ dàng cho các tác vụ gây tranh cãi dữ liệu phổ biến nhất. Nó được xây dựng để làm việc trực tiếp với các khung dữ liệu, với nhiều tác vụ phổ biến được tối ưu hóa bằng cách được viết bằng ngôn ngữ được biên dịch (C ++) (không phải tất cả các gói R đều được viết bằng R!).

## load the tidyverse
library(tidyverse)
library(here)

interviews <- read_csv(here("data", "SAFI_clean.csv"), na = "NULL")

## inspect the data
interviews

## preview the data
# view(interviews)

Gói

# filters observations with "|" logical operator
# output dataframe satisfies AT LEAST ONE of the specified conditions
filter(interviews, village == "Chirodzo" | village == "Ruaca")
0 giải quyết vấn đề chung là muốn định hình lại dữ liệu của bạn để vẽ và sử dụng bởi các chức năng R khác nhau. Đôi khi chúng tôi muốn các bộ dữ liệu nơi chúng tôi có một hàng cho mỗi lần đo. Đôi khi chúng tôi muốn một khung dữ liệu trong đó mỗi loại đo có cột riêng và các hàng thay vào đó là các nhóm tổng hợp hơn. Di chuyển qua lại giữa các định dạng này là không cần thiết và
# filters observations with "|" logical operator
# output dataframe satisfies AT LEAST ONE of the specified conditions
filter(interviews, village == "Chirodzo" | village == "Ruaca")
0 cung cấp cho bạn các công cụ cho việc gây tranh cãi dữ liệu này và tinh vi hơn.
# A tibble: 10 × 14
   key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                       
 1     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
 2     49 Chirodzo 2016-11-16 00:00:00         6        26 burntb…     2    
 3     52 Chirodzo 2016-11-16 00:00:00        11        15 burntb…     3 no     
 4     56 Chirodzo 2016-11-16 00:00:00        12        23 burntb…     2 yes    
 5     65 Chirodzo 2016-11-16 00:00:00         8        20 burntb…     3 no     
 6     66 Chirodzo 2016-11-16 00:00:00        10        37 burntb…     3 yes    
 7     67 Chirodzo 2016-11-16 00:00:00         5        31 burntb…     2 no     
 8     68 Chirodzo 2016-11-16 00:00:00         8        52 burntb…     3 no     
 9    199 Chirodzo 2017-06-04 00:00:00         7        17 burntb…     2 yes    
10    200 Chirodzo 2017-06-04 00:00:00         8        20 burntb…     2    
# … with 6 more variables: affect_conflicts , liv_count ,
#   items_owned , no_meals , months_lack_food ,
#   instanceID , and abbreviated variable names ¹​respondent_wall_type,
#   ²​memb_assoc
9
functions:

  • Nhưng cũng có các gói có sẵn cho một loạt các tác vụ bao gồm các lô xây dựng (____10102, mà chúng tôi sẽ thấy sau), tải xuống dữ liệu từ cơ sở dữ liệu NCBI hoặc thực hiện phân tích thống kê trên bộ dữ liệu của bạn. Nhiều gói như thế này được lưu trữ và có thể tải xuống từ Mạng Rarchive toàn diện (CRAN) bằng cách sử dụng
    # to select columns throughout the dataframe
    select(interviews, village, no_membrs, months_lack_food)
    # to do the same thing with subsetting
    interviews[c("village","no_membrs","months_lack_food")]
    # to select a series of connected columns
    select(interviews, village:respondent_wall_type)
    
    07. Hàm này làm cho gói có thể truy cập bằng cách cài đặt R của bạn với lệnh
    # to select columns throughout the dataframe
    select(interviews, village, no_membrs, months_lack_food)
    # to do the same thing with subsetting
    interviews[c("village","no_membrs","months_lack_food")]
    # to select a series of connected columns
    select(interviews, village:respondent_wall_type)
    
    08, như bạn đã làm với
    # to select columns throughout the dataframe
    select(interviews, village, no_membrs, months_lack_food)
    # to do the same thing with subsetting
    interviews[c("village","no_membrs","months_lack_food")]
    # to select a series of connected columns
    select(interviews, village:respondent_wall_type)
    
    09 trước đó.
  • Để dễ dàng truy cập tài liệu cho một gói trong r hoặc rstudio, hãy sử dụng
    # to select columns throughout the dataframe
    select(interviews, village, no_membrs, months_lack_food)
    # to do the same thing with subsetting
    interviews[c("village","no_membrs","months_lack_food")]
    # to select a series of connected columns
    select(interviews, village:respondent_wall_type)
    
    10.
  • Để tìm hiểu thêm về
    # A tibble: 10 × 14
       key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                           
     1     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
     2     49 Chirodzo 2016-11-16 00:00:00         6        26 burntb…     2    
     3     52 Chirodzo 2016-11-16 00:00:00        11        15 burntb…     3 no     
     4     56 Chirodzo 2016-11-16 00:00:00        12        23 burntb…     2 yes    
     5     65 Chirodzo 2016-11-16 00:00:00         8        20 burntb…     3 no     
     6     66 Chirodzo 2016-11-16 00:00:00        10        37 burntb…     3 yes    
     7     67 Chirodzo 2016-11-16 00:00:00         5        31 burntb…     2 no     
     8     68 Chirodzo 2016-11-16 00:00:00         8        52 burntb…     3 no     
     9    199 Chirodzo 2017-06-04 00:00:00         7        17 burntb…     2 yes    
    10    200 Chirodzo 2017-06-04 00:00:00         8        20 burntb…     2    
    # … with 6 more variables: affect_conflicts , liv_count ,
    #   items_owned , no_meals , months_lack_food ,
    #   instanceID , and abbreviated variable names ¹​respondent_wall_type,
    #   ²​memb_assoc
    
    9 và
    # filters observations with "|" logical operator
    # output dataframe satisfies AT LEAST ONE of the specified conditions
    filter(interviews, village == "Chirodzo" | village == "Ruaca")
    
    0 sau hội thảo, bạn có thể muốn xem chuyển đổi dữ liệu tiện dụng này với
    # A tibble: 10 × 14
       key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                           
     1     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
     2     49 Chirodzo 2016-11-16 00:00:00         6        26 burntb…     2    
     3     52 Chirodzo 2016-11-16 00:00:00        11        15 burntb…     3 no     
     4     56 Chirodzo 2016-11-16 00:00:00        12        23 burntb…     2 yes    
     5     65 Chirodzo 2016-11-16 00:00:00         8        20 burntb…     3 no     
     6     66 Chirodzo 2016-11-16 00:00:00        10        37 burntb…     3 yes    
     7     67 Chirodzo 2016-11-16 00:00:00         5        31 burntb…     2 no     
     8     68 Chirodzo 2016-11-16 00:00:00         8        52 burntb…     3 no     
     9    199 Chirodzo 2017-06-04 00:00:00         7        17 burntb…     2 yes    
    10    200 Chirodzo 2017-06-04 00:00:00         8        20 burntb…     2    
    # … with 6 more variables: affect_conflicts , liv_count ,
    #   items_owned , no_meals , months_lack_food ,
    #   instanceID , and abbreviated variable names ¹​respondent_wall_type,
    #   ²​memb_assoc
    
    9 CheatSheet và cái này về
    # filters observations with "|" logical operator
    # output dataframe satisfies AT LEAST ONE of the specified conditions
    filter(interviews, village == "Chirodzo" | village == "Ruaca")
    
    0.
  • Có những lựa chọn thay thế cho các gói
    # to select columns throughout the dataframe
    select(interviews, village, no_membrs, months_lack_food)
    # to do the same thing with subsetting
    interviews[c("village","no_membrs","months_lack_food")]
    # to select a series of connected columns
    select(interviews, village:respondent_wall_type)
    
    09 cho dữ liệu gây tranh cãi, bao gồm cả gói
    # to select columns throughout the dataframe
    select(interviews, village, no_membrs, months_lack_food)
    # to do the same thing with subsetting
    interviews[c("village","no_membrs","months_lack_food")]
    # to select a series of connected columns
    select(interviews, village:respondent_wall_type)
    
    16. Xem so sánh này ví dụ để hiểu được sự khác biệt giữa việc sử dụng
    # to select columns throughout the dataframe
    select(interviews, village, no_membrs, months_lack_food)
    # to do the same thing with subsetting
    interviews[c("village","no_membrs","months_lack_food")]
    # to select a series of connected columns
    select(interviews, village:respondent_wall_type)
    
    17,
    # to select columns throughout the dataframe
    select(interviews, village, no_membrs, months_lack_food)
    # to do the same thing with subsetting
    interviews[c("village","no_membrs","months_lack_food")]
    # to select a series of connected columns
    select(interviews, village:respondent_wall_type)
    
    09 và
    # to select columns throughout the dataframe
    select(interviews, village, no_membrs, months_lack_food)
    # to do the same thing with subsetting
    interviews[c("village","no_membrs","months_lack_food")]
    # to select a series of connected columns
    select(interviews, village:respondent_wall_type)
    
    16.
  • Học
    # A tibble: 10 × 14
       key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                           
     1     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
     2     49 Chirodzo 2016-11-16 00:00:00         6        26 burntb…     2    
     3     52 Chirodzo 2016-11-16 00:00:00        11        15 burntb…     3 no     
     4     56 Chirodzo 2016-11-16 00:00:00        12        23 burntb…     2 yes    
     5     65 Chirodzo 2016-11-16 00:00:00         8        20 burntb…     3 no     
     6     66 Chirodzo 2016-11-16 00:00:00        10        37 burntb…     3 yes    
     7     67 Chirodzo 2016-11-16 00:00:00         5        31 burntb…     2 no     
     8     68 Chirodzo 2016-11-16 00:00:00         8        52 burntb…     3 no     
     9    199 Chirodzo 2017-06-04 00:00:00         7        17 burntb…     2 yes    
    10    200 Chirodzo 2017-06-04 00:00:00         8        20 burntb…     2    
    # … with 6 more variables: affect_conflicts , liv_count ,
    #   items_owned , no_meals , months_lack_food ,
    #   instanceID , and abbreviated variable names ¹​respondent_wall_type,
    #   ²​memb_assoc
    
    9 và
    # filters observations with "|" logical operator
    # output dataframe satisfies AT LEAST ONE of the specified conditions
    filter(interviews, village == "Chirodzo" | village == "Ruaca")
    
    0
  • Để đảm bảo mọi người sẽ sử dụng cùng một bộ dữ liệu cho bài học này, chúng tôi sẽ đọc lại bộ dữ liệu SAFI mà chúng tôi đã tải xuống trước đó.

Chúng tôi sẽ học một số chức năng # A tibble: 10 × 14 key_ID village interview_date no_membrs years_liv respo…¹ rooms memb_…² 1 10 Chirodzo 2016-12-16 00:00:00 12 23 burntb… 5 no 2 49 Chirodzo 2016-11-16 00:00:00 6 26 burntb… 2 3 52 Chirodzo 2016-11-16 00:00:00 11 15 burntb… 3 no 4 56 Chirodzo 2016-11-16 00:00:00 12 23 burntb… 2 yes 5 65 Chirodzo 2016-11-16 00:00:00 8 20 burntb… 3 no 6 66 Chirodzo 2016-11-16 00:00:00 10 37 burntb… 3 yes 7 67 Chirodzo 2016-11-16 00:00:00 5 31 burntb… 2 no 8 68 Chirodzo 2016-11-16 00:00:00 8 52 burntb… 3 no 9 199 Chirodzo 2017-06-04 00:00:00 7 17 burntb… 2 yes 10 200 Chirodzo 2017-06-04 00:00:00 8 20 burntb… 2 # … with 6 more variables: affect_conflicts , liv_count , # items_owned , no_meals , months_lack_food , # instanceID , and abbreviated variable names ¹​respondent_wall_type, # ²​memb_assoc 9 phổ biến nhất:

# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
23: Cột tập con

# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)

Để chọn các hàng dựa trên các tiêu chí cụ thể, chúng ta có thể sử dụng hàm

# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
24. Đối số sau khi DataFrame là điều kiện chúng tôi muốn khung dữ liệu cuối cùng của chúng tôi tuân thủ (ví dụ: tên làng là chirodzo):

# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")

# A tibble: 39 × 14
   key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                       
 1      8 Chirodzo 2016-11-16 00:00:00        12        70 burntb…     3 yes    
 2      9 Chirodzo 2016-11-16 00:00:00         8         6 burntb…     1 no     
 3     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
 4     34 Chirodzo 2016-11-17 00:00:00         8        18 burntb…     3 yes    
 5     35 Chirodzo 2016-11-17 00:00:00         5        45 muddaub     1 yes    
 6     36 Chirodzo 2016-11-17 00:00:00         6        23 sunbri…     1 yes    
 7     37 Chirodzo 2016-11-17 00:00:00         3         8 burntb…     1    
 8     43 Chirodzo 2016-11-17 00:00:00         7        29 muddaub     1 no     
 9     44 Chirodzo 2016-11-17 00:00:00         2         6 muddaub     1    
10     45 Chirodzo 2016-11-17 00:00:00         9         7 muddaub     1 no     
# … with 29 more rows, 6 more variables: affect_conflicts ,
#   liv_count , items_owned , no_meals , months_lack_food ,
#   instanceID , and abbreviated variable names ¹​respondent_wall_type,
#   ²​memb_assoc

Chúng tôi cũng có thể chỉ định nhiều điều kiện trong hàm

# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
24. Chúng ta có thể kết hợp các điều kiện bằng cách sử dụng các câu lệnh và hoặc hoặc hoặc các câu lệnh. Trong một tuyên bố của người Viking và một người quan sát (hàng) phải đáp ứng mọi tiêu chí để được đưa vào khung dữ liệu kết quả. Để hình thành các câu lệnh và các câu lệnh trong DPPLYR, chúng ta có thể vượt qua các điều kiện mong muốn của mình dưới dạng các đối số trong hàm
# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
24, được phân tách bằng dấu phẩy:every criteria to be included in the resulting dataframe. To form “and” statements within dplyr, we can pass our desired conditions as arguments in the
# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
24 function, separated by commas:

# filters observations with "and" operator (comma)
# output dataframe satisfies ALL specified conditions
filter(interviews, village == "Chirodzo", 
                   rooms > 1, 
                   no_meals > 2)

# A tibble: 10 × 14
   key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                       
 1     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
 2     49 Chirodzo 2016-11-16 00:00:00         6        26 burntb…     2    
 3     52 Chirodzo 2016-11-16 00:00:00        11        15 burntb…     3 no     
 4     56 Chirodzo 2016-11-16 00:00:00        12        23 burntb…     2 yes    
 5     65 Chirodzo 2016-11-16 00:00:00         8        20 burntb…     3 no     
 6     66 Chirodzo 2016-11-16 00:00:00        10        37 burntb…     3 yes    
 7     67 Chirodzo 2016-11-16 00:00:00         5        31 burntb…     2 no     
 8     68 Chirodzo 2016-11-16 00:00:00         8        52 burntb…     3 no     
 9    199 Chirodzo 2017-06-04 00:00:00         7        17 burntb…     2 yes    
10    200 Chirodzo 2017-06-04 00:00:00         8        20 burntb…     2    
# … with 6 more variables: affect_conflicts , liv_count ,
#   items_owned , no_meals , months_lack_food ,
#   instanceID , and abbreviated variable names ¹​respondent_wall_type,
#   ²​memb_assoc

Chúng tôi cũng có thể hình thành các câu lệnh của người Viking và trực tiếp với toán tử

# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
38 thay vì dấu phẩy:

# filters observations with "&" logical operator
# output dataframe satisfies ALL specified conditions
filter(interviews, village == "Chirodzo" & 
                   rooms > 1 & 
                   no_meals > 2)

# A tibble: 10 × 14
   key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                       
 1     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
 2     49 Chirodzo 2016-11-16 00:00:00         6        26 burntb…     2    
 3     52 Chirodzo 2016-11-16 00:00:00        11        15 burntb…     3 no     
 4     56 Chirodzo 2016-11-16 00:00:00        12        23 burntb…     2 yes    
 5     65 Chirodzo 2016-11-16 00:00:00         8        20 burntb…     3 no     
 6     66 Chirodzo 2016-11-16 00:00:00        10        37 burntb…     3 yes    
 7     67 Chirodzo 2016-11-16 00:00:00         5        31 burntb…     2 no     
 8     68 Chirodzo 2016-11-16 00:00:00         8        52 burntb…     3 no     
 9    199 Chirodzo 2017-06-04 00:00:00         7        17 burntb…     2 yes    
10    200 Chirodzo 2017-06-04 00:00:00         8        20 burntb…     2    
# … with 6 more variables: affect_conflicts , liv_count ,
#   items_owned , no_meals , months_lack_food ,
#   instanceID , and abbreviated variable names ¹​respondent_wall_type,
#   ²​memb_assoc

Trong một tuyên bố của người khác, các quan sát phải đáp ứng ít nhất một trong các điều kiện được chỉ định. Để hình thành các câu lệnh hoặc các câu lệnh, chúng tôi sử dụng toán tử logic cho các loại hoặc, đó là thanh dọc (|):

# filters observations with "|" logical operator
# output dataframe satisfies AT LEAST ONE of the specified conditions
filter(interviews, village == "Chirodzo" | village == "Ruaca")

# A tibble: 88 × 14
   key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                       
 1      8 Chirodzo 2016-11-16 00:00:00        12        70 burntb…     3 yes    
 2      9 Chirodzo 2016-11-16 00:00:00         8         6 burntb…     1 no     
 3     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
 4     23 Ruaca    2016-11-21 00:00:00        10        20 burntb…     4    
 5     24 Ruaca    2016-11-21 00:00:00         6         4 burntb…     2 no     
 6     25 Ruaca    2016-11-21 00:00:00        11         6 burntb…     3 no     
 7     26 Ruaca    2016-11-21 00:00:00         3        20 burntb…     2 no     
 8     27 Ruaca    2016-11-21 00:00:00         7        36 burntb…     2    
 9     28 Ruaca    2016-11-21 00:00:00         2         2 muddaub     1 no     
10     29 Ruaca    2016-11-21 00:00:00         7        10 burntb…     2 yes    
# … with 78 more rows, 6 more variables: affect_conflicts ,
#   liv_count , items_owned , no_meals , months_lack_food ,
#   instanceID , and abbreviated variable names ¹​respondent_wall_type,
#   ²​memb_assoc

Ống

Điều gì sẽ xảy ra nếu bạn muốn chọn và lọc cùng một lúc? Có ba cách để làm điều này: sử dụng các bước trung gian, chức năng lồng nhau hoặc đường ống.

Với các bước trung gian, bạn tạo một khung dữ liệu tạm thời và sử dụng nó làm đầu vào cho chức năng tiếp theo, như thế này:

# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
0

Điều này có thể đọc được, nhưng có thể làm lộn xộn không gian làm việc của bạn với rất nhiều đối tượng mà bạn phải đặt tên riêng lẻ. Với nhiều bước, điều đó có thể khó theo dõi.

Bạn cũng có thể làm tổ các chức năng (nghĩa là một chức năng bên trong một chức năng khác), như thế này:

# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
1

Điều này rất tiện dụng, nhưng có thể khó đọc nếu quá nhiều chức năng được lồng, vì R đánh giá biểu thức từ trong ra ngoài (trong trường hợp này, lọc, sau đó chọn).

Tùy chọn cuối cùng, đường ống, là một bổ sung gần đây cho R. Pipes cho phép bạn lấy đầu ra của một hàm và gửi trực tiếp đến tiếp theo, điều này rất hữu ích khi bạn cần làm nhiều việc cho cùng một bộ dữ liệu. Các đường ống trong r trông giống như

# filters observations with "|" logical operator
# output dataframe satisfies AT LEAST ONE of the specified conditions
filter(interviews, village == "Chirodzo" | village == "Ruaca")
6 và được cung cấp thông qua gói
# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
40, được cài đặt tự động với
# A tibble: 10 × 14
   key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                       
 1     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
 2     49 Chirodzo 2016-11-16 00:00:00         6        26 burntb…     2    
 3     52 Chirodzo 2016-11-16 00:00:00        11        15 burntb…     3 no     
 4     56 Chirodzo 2016-11-16 00:00:00        12        23 burntb…     2 yes    
 5     65 Chirodzo 2016-11-16 00:00:00         8        20 burntb…     3 no     
 6     66 Chirodzo 2016-11-16 00:00:00        10        37 burntb…     3 yes    
 7     67 Chirodzo 2016-11-16 00:00:00         5        31 burntb…     2 no     
 8     68 Chirodzo 2016-11-16 00:00:00         8        52 burntb…     3 no     
 9    199 Chirodzo 2017-06-04 00:00:00         7        17 burntb…     2 yes    
10    200 Chirodzo 2017-06-04 00:00:00         8        20 burntb…     2    
# … with 6 more variables: affect_conflicts , liv_count ,
#   items_owned , no_meals , months_lack_food ,
#   instanceID , and abbreviated variable names ¹​respondent_wall_type,
#   ²​memb_assoc
9. Nếu bạn sử dụng rstudio, bạn có thể nhập ống bằng:
# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
40
package, installed automatically with
# A tibble: 10 × 14
   key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                       
 1     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
 2     49 Chirodzo 2016-11-16 00:00:00         6        26 burntb…     2    
 3     52 Chirodzo 2016-11-16 00:00:00        11        15 burntb…     3 no     
 4     56 Chirodzo 2016-11-16 00:00:00        12        23 burntb…     2 yes    
 5     65 Chirodzo 2016-11-16 00:00:00         8        20 burntb…     3 no     
 6     66 Chirodzo 2016-11-16 00:00:00        10        37 burntb…     3 yes    
 7     67 Chirodzo 2016-11-16 00:00:00         5        31 burntb…     2 no     
 8     68 Chirodzo 2016-11-16 00:00:00         8        52 burntb…     3 no     
 9    199 Chirodzo 2017-06-04 00:00:00         7        17 burntb…     2 yes    
10    200 Chirodzo 2017-06-04 00:00:00         8        20 burntb…     2    
# … with 6 more variables: affect_conflicts , liv_count ,
#   items_owned , no_meals , months_lack_food ,
#   instanceID , and abbreviated variable names ¹​respondent_wall_type,
#   ²​memb_assoc
9
. If you use RStudio, you can type the pipe with:

  • Ctrl + Shift + M Nếu bạn có PC hoặc CMD + Shift + M nếu bạn có máy Mac.

# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
2

# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
3

Trong mã trên, chúng tôi sử dụng đường ống để gửi bộ dữ liệu

# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
31 trước thông qua
# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
24 để giữ các hàng trong đó
# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
44 là chirodzo, sau đó thông qua
# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
23 để chỉ giữ các cột từ
# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
44 đến
# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
47. Vì
# filters observations with "|" logical operator
# output dataframe satisfies AT LEAST ONE of the specified conditions
filter(interviews, village == "Chirodzo" | village == "Ruaca")
6 lấy đối tượng bên trái và chuyển nó làm đối số đầu tiên cho hàm bên phải của nó, chúng tôi không cần phải bao gồm một cách rõ ràng khung dữ liệu như một đối số cho các chức năng
# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
24 và
# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
23 nữa.

Một số người có thể thấy hữu ích khi đọc đường ống giống như từ ngữ sau đó là. Chẳng hạn, trong ví dụ trên, chúng tôi lấy DataFrame

# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
31, sau đó chúng tôi
# filters observations with "|" logical operator
# output dataframe satisfies AT LEAST ONE of the specified conditions
filter(interviews, village == "Chirodzo" | village == "Ruaca")
4 cho các hàng có
# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
53, sau đó chúng tôi
# filters observations with "|" logical operator
# output dataframe satisfies AT LEAST ONE of the specified conditions
filter(interviews, village == "Chirodzo" | village == "Ruaca")
2 cột
# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
55. Các chức năng
# A tibble: 10 × 14
   key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                       
 1     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
 2     49 Chirodzo 2016-11-16 00:00:00         6        26 burntb…     2    
 3     52 Chirodzo 2016-11-16 00:00:00        11        15 burntb…     3 no     
 4     56 Chirodzo 2016-11-16 00:00:00        12        23 burntb…     2 yes    
 5     65 Chirodzo 2016-11-16 00:00:00         8        20 burntb…     3 no     
 6     66 Chirodzo 2016-11-16 00:00:00        10        37 burntb…     3 yes    
 7     67 Chirodzo 2016-11-16 00:00:00         5        31 burntb…     2 no     
 8     68 Chirodzo 2016-11-16 00:00:00         8        52 burntb…     3 no     
 9    199 Chirodzo 2017-06-04 00:00:00         7        17 burntb…     2 yes    
10    200 Chirodzo 2017-06-04 00:00:00         8        20 burntb…     2    
# … with 6 more variables: affect_conflicts , liv_count ,
#   items_owned , no_meals , months_lack_food ,
#   instanceID , and abbreviated variable names ¹​respondent_wall_type,
#   ²​memb_assoc
9 một mình có phần đơn giản, nhưng bằng cách kết hợp chúng thành các quy trình công việc tuyến tính với đường ống, chúng ta có thể thực hiện các hoạt động gây tranh cãi dữ liệu phức tạp hơn.
# A tibble: 10 × 14
   key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                       
 1     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
 2     49 Chirodzo 2016-11-16 00:00:00         6        26 burntb…     2    
 3     52 Chirodzo 2016-11-16 00:00:00        11        15 burntb…     3 no     
 4     56 Chirodzo 2016-11-16 00:00:00        12        23 burntb…     2 yes    
 5     65 Chirodzo 2016-11-16 00:00:00         8        20 burntb…     3 no     
 6     66 Chirodzo 2016-11-16 00:00:00        10        37 burntb…     3 yes    
 7     67 Chirodzo 2016-11-16 00:00:00         5        31 burntb…     2 no     
 8     68 Chirodzo 2016-11-16 00:00:00         8        52 burntb…     3 no     
 9    199 Chirodzo 2017-06-04 00:00:00         7        17 burntb…     2 yes    
10    200 Chirodzo 2017-06-04 00:00:00         8        20 burntb…     2    
# … with 6 more variables: affect_conflicts , liv_count ,
#   items_owned , no_meals , months_lack_food ,
#   instanceID , and abbreviated variable names ¹​respondent_wall_type,
#   ²​memb_assoc
9
functions by themselves are somewhat simple, but by combining them into linear workflows with the pipe, we can accomplish more complex data wrangling operations.

Nếu chúng ta muốn tạo một đối tượng mới với phiên bản dữ liệu nhỏ hơn này, chúng ta có thể gán cho nó một tên mới:

# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
4

# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
3

Lưu ý rằng DataFrame cuối cùng (

# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
57) là phần ngoài cùng bên trái của biểu thức này.

Tập thể dục

Sử dụng các đường ống, tập hợp dữ liệu

# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
31 để bao gồm các cuộc phỏng vấn trong đó người trả lời là thành viên của hiệp hội thủy lợi (
# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
59) và chỉ giữ lại các cột
# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
60,
# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
61 và
# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
62.

Dung dịch

# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
6

# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
7

Biến đổi

Thông thường, bạn sẽ muốn tạo các cột mới dựa trên các giá trị trong các cột hiện có, ví dụ như thực hiện chuyển đổi đơn vị hoặc tìm tỷ lệ của các giá trị trong hai cột. Đối với điều này, chúng tôi sẽ sử dụng

# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
25.

Chúng tôi có thể quan tâm đến tỷ lệ số lượng thành viên trong gia đình với các phòng được sử dụng để ngủ (tức là số người AVG mỗi phòng):

# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
8

# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
9

Chúng tôi có thể quan tâm đến việc điều tra xem việc trở thành thành viên của một hiệp hội thủy lợi có ảnh hưởng gì đến tỷ lệ các thành viên trong gia đình với phòng hay không. Để xem xét mối quan hệ này, trước tiên chúng tôi sẽ xóa dữ liệu khỏi bộ dữ liệu của chúng tôi, nơi người trả lời đã không trả lời câu hỏi liệu họ có phải là thành viên của một hiệp hội thủy lợi hay không. Những trường hợp này được ghi lại là Null Null trong bộ dữ liệu.

Để loại bỏ những trường hợp này, chúng ta có thể chèn một

# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
24 trong chuỗi:

# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
0

# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
1

Biểu tượng

# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
65 phủ nhận kết quả của hàm
# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
66. Do đó, nếu
# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
66 trả về giá trị
# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
68 (vì thiếu
# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
59), biểu tượng
# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
65 phủ nhận điều này và nói rằng chúng tôi chỉ muốn các giá trị của
# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
71, trong đó không bị thiếu.is not missing.

Tập thể dục

Sử dụng các đường ống, tập hợp dữ liệu

# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
31 để bao gồm các cuộc phỏng vấn trong đó người trả lời là thành viên của hiệp hội thủy lợi (
# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
59) và chỉ giữ lại các cột
# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
60,
# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
61 và
# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
62.

Gợi ý: Hãy suy nghĩ về cách các lệnh nên được yêu cầu tạo ra khung dữ liệu này!: think about how the commands should be ordered to produce this data frame!

Dung dịch

# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
2

Phân tích dữ liệu-bombine phân tách và hàm tóm tắt ()

Nhiều nhiệm vụ phân tích dữ liệu có thể được tiếp cận bằng cách sử dụng mô hình-bombine phân chia: chia dữ liệu thành các nhóm, áp dụng một số phân tích cho từng nhóm và sau đó kết hợp kết quả.

# A tibble: 10 × 14
   key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                       
 1     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
 2     49 Chirodzo 2016-11-16 00:00:00         6        26 burntb…     2    
 3     52 Chirodzo 2016-11-16 00:00:00        11        15 burntb…     3 no     
 4     56 Chirodzo 2016-11-16 00:00:00        12        23 burntb…     2 yes    
 5     65 Chirodzo 2016-11-16 00:00:00         8        20 burntb…     3 no     
 6     66 Chirodzo 2016-11-16 00:00:00        10        37 burntb…     3 yes    
 7     67 Chirodzo 2016-11-16 00:00:00         5        31 burntb…     2 no     
 8     68 Chirodzo 2016-11-16 00:00:00         8        52 burntb…     3 no     
 9    199 Chirodzo 2017-06-04 00:00:00         7        17 burntb…     2 yes    
10    200 Chirodzo 2017-06-04 00:00:00         8        20 burntb…     2    
# … with 6 more variables: affect_conflicts , liv_count ,
#   items_owned , no_meals , months_lack_food ,
#   instanceID , and abbreviated variable names ¹​respondent_wall_type,
#   ²​memb_assoc
9 làm cho điều này rất dễ dàng thông qua việc sử dụng hàm
# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
26.
# A tibble: 10 × 14
   key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                       
 1     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
 2     49 Chirodzo 2016-11-16 00:00:00         6        26 burntb…     2    
 3     52 Chirodzo 2016-11-16 00:00:00        11        15 burntb…     3 no     
 4     56 Chirodzo 2016-11-16 00:00:00        12        23 burntb…     2 yes    
 5     65 Chirodzo 2016-11-16 00:00:00         8        20 burntb…     3 no     
 6     66 Chirodzo 2016-11-16 00:00:00        10        37 burntb…     3 yes    
 7     67 Chirodzo 2016-11-16 00:00:00         5        31 burntb…     2 no     
 8     68 Chirodzo 2016-11-16 00:00:00         8        52 burntb…     3 no     
 9    199 Chirodzo 2017-06-04 00:00:00         7        17 burntb…     2 yes    
10    200 Chirodzo 2017-06-04 00:00:00         8        20 burntb…     2    
# … with 6 more variables: affect_conflicts , liv_count ,
#   items_owned , no_meals , months_lack_food ,
#   instanceID , and abbreviated variable names ¹​respondent_wall_type,
#   ²​memb_assoc
9
makes this very easy through the use of the
# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
26 function.

Hàm
# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
27

# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
26 thường được sử dụng cùng với
# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
27, thu gọn từng nhóm thành một bản tóm tắt một hàng của nhóm đó.
# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
26 lấy làm đối số Các tên cột có chứa các biến phân loại mà bạn muốn tính toán số liệu thống kê tóm tắt. Vì vậy, để tính toán quy mô hộ gia đình trung bình của làng:categorical variables for which you want to calculate the summary statistics. So to compute the average household size by village:

# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
3

# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
4

Bạn cũng có thể nhận thấy rằng đầu ra từ các cuộc gọi này không chạy khỏi màn hình nữa. Nó là một trong những lợi thế của

# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
85 so với DataFrame.

Bạn cũng có thể nhóm theo nhiều cột:

# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
5

# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
6

# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
7

Lưu ý rằng đầu ra là một tibbble được nhóm. Để có được một tibbble không có nhóm, hãy sử dụng hàm

# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
86:

# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
8

# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
6

# A tibble: 39 × 14
   key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                       
 1      8 Chirodzo 2016-11-16 00:00:00        12        70 burntb…     3 yes    
 2      9 Chirodzo 2016-11-16 00:00:00         8         6 burntb…     1 no     
 3     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
 4     34 Chirodzo 2016-11-17 00:00:00         8        18 burntb…     3 yes    
 5     35 Chirodzo 2016-11-17 00:00:00         5        45 muddaub     1 yes    
 6     36 Chirodzo 2016-11-17 00:00:00         6        23 sunbri…     1 yes    
 7     37 Chirodzo 2016-11-17 00:00:00         3         8 burntb…     1    
 8     43 Chirodzo 2016-11-17 00:00:00         7        29 muddaub     1 no     
 9     44 Chirodzo 2016-11-17 00:00:00         2         6 muddaub     1    
10     45 Chirodzo 2016-11-17 00:00:00         9         7 muddaub     1 no     
# … with 29 more rows, 6 more variables: affect_conflicts ,
#   liv_count , items_owned , no_meals , months_lack_food ,
#   instanceID , and abbreviated variable names ¹​respondent_wall_type,
#   ²​memb_assoc
0

Khi nhóm cả bằng

# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
44 và
# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
88, chúng tôi thấy các hàng trong bảng của chúng tôi dành cho những người được hỏi không xác định liệu chúng có phải là thành viên của một hiệp hội thủy lợi hay không. Chúng tôi có thể loại trừ những dữ liệu đó khỏi bảng của chúng tôi bằng bước lọc.

# A tibble: 39 × 14
   key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                       
 1      8 Chirodzo 2016-11-16 00:00:00        12        70 burntb…     3 yes    
 2      9 Chirodzo 2016-11-16 00:00:00         8         6 burntb…     1 no     
 3     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
 4     34 Chirodzo 2016-11-17 00:00:00         8        18 burntb…     3 yes    
 5     35 Chirodzo 2016-11-17 00:00:00         5        45 muddaub     1 yes    
 6     36 Chirodzo 2016-11-17 00:00:00         6        23 sunbri…     1 yes    
 7     37 Chirodzo 2016-11-17 00:00:00         3         8 burntb…     1    
 8     43 Chirodzo 2016-11-17 00:00:00         7        29 muddaub     1 no     
 9     44 Chirodzo 2016-11-17 00:00:00         2         6 muddaub     1    
10     45 Chirodzo 2016-11-17 00:00:00         9         7 muddaub     1 no     
# … with 29 more rows, 6 more variables: affect_conflicts ,
#   liv_count , items_owned , no_meals , months_lack_food ,
#   instanceID , and abbreviated variable names ¹​respondent_wall_type,
#   ²​memb_assoc
1

# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
6

# A tibble: 39 × 14
   key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                       
 1      8 Chirodzo 2016-11-16 00:00:00        12        70 burntb…     3 yes    
 2      9 Chirodzo 2016-11-16 00:00:00         8         6 burntb…     1 no     
 3     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
 4     34 Chirodzo 2016-11-17 00:00:00         8        18 burntb…     3 yes    
 5     35 Chirodzo 2016-11-17 00:00:00         5        45 muddaub     1 yes    
 6     36 Chirodzo 2016-11-17 00:00:00         6        23 sunbri…     1 yes    
 7     37 Chirodzo 2016-11-17 00:00:00         3         8 burntb…     1    
 8     43 Chirodzo 2016-11-17 00:00:00         7        29 muddaub     1 no     
 9     44 Chirodzo 2016-11-17 00:00:00         2         6 muddaub     1    
10     45 Chirodzo 2016-11-17 00:00:00         9         7 muddaub     1 no     
# … with 29 more rows, 6 more variables: affect_conflicts ,
#   liv_count , items_owned , no_meals , months_lack_food ,
#   instanceID , and abbreviated variable names ¹​respondent_wall_type,
#   ²​memb_assoc
3

Khi dữ liệu được nhóm lại, bạn cũng có thể tóm tắt nhiều biến cùng một lúc (và không nhất thiết phải trên cùng một biến). Chẳng hạn, chúng tôi có thể thêm một cột cho biết quy mô hộ gia đình tối thiểu cho mỗi làng cho mỗi nhóm (thành viên của một hiệp hội thủy lợi so với không):

# A tibble: 39 × 14
   key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                       
 1      8 Chirodzo 2016-11-16 00:00:00        12        70 burntb…     3 yes    
 2      9 Chirodzo 2016-11-16 00:00:00         8         6 burntb…     1 no     
 3     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
 4     34 Chirodzo 2016-11-17 00:00:00         8        18 burntb…     3 yes    
 5     35 Chirodzo 2016-11-17 00:00:00         5        45 muddaub     1 yes    
 6     36 Chirodzo 2016-11-17 00:00:00         6        23 sunbri…     1 yes    
 7     37 Chirodzo 2016-11-17 00:00:00         3         8 burntb…     1    
 8     43 Chirodzo 2016-11-17 00:00:00         7        29 muddaub     1 no     
 9     44 Chirodzo 2016-11-17 00:00:00         2         6 muddaub     1    
10     45 Chirodzo 2016-11-17 00:00:00         9         7 muddaub     1 no     
# … with 29 more rows, 6 more variables: affect_conflicts ,
#   liv_count , items_owned , no_meals , months_lack_food ,
#   instanceID , and abbreviated variable names ¹​respondent_wall_type,
#   ²​memb_assoc
4

# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
6

# A tibble: 39 × 14
   key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                       
 1      8 Chirodzo 2016-11-16 00:00:00        12        70 burntb…     3 yes    
 2      9 Chirodzo 2016-11-16 00:00:00         8         6 burntb…     1 no     
 3     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
 4     34 Chirodzo 2016-11-17 00:00:00         8        18 burntb…     3 yes    
 5     35 Chirodzo 2016-11-17 00:00:00         5        45 muddaub     1 yes    
 6     36 Chirodzo 2016-11-17 00:00:00         6        23 sunbri…     1 yes    
 7     37 Chirodzo 2016-11-17 00:00:00         3         8 burntb…     1    
 8     43 Chirodzo 2016-11-17 00:00:00         7        29 muddaub     1 no     
 9     44 Chirodzo 2016-11-17 00:00:00         2         6 muddaub     1    
10     45 Chirodzo 2016-11-17 00:00:00         9         7 muddaub     1 no     
# … with 29 more rows, 6 more variables: affect_conflicts ,
#   liv_count , items_owned , no_meals , months_lack_food ,
#   instanceID , and abbreviated variable names ¹​respondent_wall_type,
#   ²​memb_assoc
6

Đôi khi rất hữu ích để sắp xếp lại kết quả của một truy vấn để kiểm tra các giá trị. Chẳng hạn, chúng tôi có thể sắp xếp trên

# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
89 để đưa nhóm với hộ gia đình nhỏ nhất trước:

# A tibble: 39 × 14
   key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                       
 1      8 Chirodzo 2016-11-16 00:00:00        12        70 burntb…     3 yes    
 2      9 Chirodzo 2016-11-16 00:00:00         8         6 burntb…     1 no     
 3     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
 4     34 Chirodzo 2016-11-17 00:00:00         8        18 burntb…     3 yes    
 5     35 Chirodzo 2016-11-17 00:00:00         5        45 muddaub     1 yes    
 6     36 Chirodzo 2016-11-17 00:00:00         6        23 sunbri…     1 yes    
 7     37 Chirodzo 2016-11-17 00:00:00         3         8 burntb…     1    
 8     43 Chirodzo 2016-11-17 00:00:00         7        29 muddaub     1 no     
 9     44 Chirodzo 2016-11-17 00:00:00         2         6 muddaub     1    
10     45 Chirodzo 2016-11-17 00:00:00         9         7 muddaub     1 no     
# … with 29 more rows, 6 more variables: affect_conflicts ,
#   liv_count , items_owned , no_meals , months_lack_food ,
#   instanceID , and abbreviated variable names ¹​respondent_wall_type,
#   ²​memb_assoc
7

# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
6

# A tibble: 39 × 14
   key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                       
 1      8 Chirodzo 2016-11-16 00:00:00        12        70 burntb…     3 yes    
 2      9 Chirodzo 2016-11-16 00:00:00         8         6 burntb…     1 no     
 3     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
 4     34 Chirodzo 2016-11-17 00:00:00         8        18 burntb…     3 yes    
 5     35 Chirodzo 2016-11-17 00:00:00         5        45 muddaub     1 yes    
 6     36 Chirodzo 2016-11-17 00:00:00         6        23 sunbri…     1 yes    
 7     37 Chirodzo 2016-11-17 00:00:00         3         8 burntb…     1    
 8     43 Chirodzo 2016-11-17 00:00:00         7        29 muddaub     1 no     
 9     44 Chirodzo 2016-11-17 00:00:00         2         6 muddaub     1    
10     45 Chirodzo 2016-11-17 00:00:00         9         7 muddaub     1 no     
# … with 29 more rows, 6 more variables: affect_conflicts ,
#   liv_count , items_owned , no_meals , months_lack_food ,
#   instanceID , and abbreviated variable names ¹​respondent_wall_type,
#   ²​memb_assoc
9

Để sắp xếp theo thứ tự giảm dần, chúng ta cần thêm hàm

# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
90. Nếu chúng ta muốn sắp xếp kết quả bằng cách giảm thứ tự quy mô hộ gia đình tối thiểu:

# filters observations with "and" operator (comma)
# output dataframe satisfies ALL specified conditions
filter(interviews, village == "Chirodzo", 
                   rooms > 1, 
                   no_meals > 2)
0

# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
6

# filters observations with "and" operator (comma)
# output dataframe satisfies ALL specified conditions
filter(interviews, village == "Chirodzo", 
                   rooms > 1, 
                   no_meals > 2)
2

Đếm

Khi làm việc với dữ liệu, chúng tôi thường muốn biết số lượng quan sát được tìm thấy cho từng yếu tố hoặc sự kết hợp của các yếu tố. Đối với nhiệm vụ này,

# A tibble: 10 × 14
   key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                       
 1     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
 2     49 Chirodzo 2016-11-16 00:00:00         6        26 burntb…     2    
 3     52 Chirodzo 2016-11-16 00:00:00        11        15 burntb…     3 no     
 4     56 Chirodzo 2016-11-16 00:00:00        12        23 burntb…     2 yes    
 5     65 Chirodzo 2016-11-16 00:00:00         8        20 burntb…     3 no     
 6     66 Chirodzo 2016-11-16 00:00:00        10        37 burntb…     3 yes    
 7     67 Chirodzo 2016-11-16 00:00:00         5        31 burntb…     2 no     
 8     68 Chirodzo 2016-11-16 00:00:00         8        52 burntb…     3 no     
 9    199 Chirodzo 2017-06-04 00:00:00         7        17 burntb…     2 yes    
10    200 Chirodzo 2017-06-04 00:00:00         8        20 burntb…     2    
# … with 6 more variables: affect_conflicts , liv_count ,
#   items_owned , no_meals , months_lack_food ,
#   instanceID , and abbreviated variable names ¹​respondent_wall_type,
#   ²​memb_assoc
9 cung cấp
# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
29. Ví dụ: nếu chúng tôi muốn đếm số lượng hàng dữ liệu cho mỗi làng, chúng tôi sẽ làm:
# A tibble: 10 × 14
   key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                       
 1     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
 2     49 Chirodzo 2016-11-16 00:00:00         6        26 burntb…     2    
 3     52 Chirodzo 2016-11-16 00:00:00        11        15 burntb…     3 no     
 4     56 Chirodzo 2016-11-16 00:00:00        12        23 burntb…     2 yes    
 5     65 Chirodzo 2016-11-16 00:00:00         8        20 burntb…     3 no     
 6     66 Chirodzo 2016-11-16 00:00:00        10        37 burntb…     3 yes    
 7     67 Chirodzo 2016-11-16 00:00:00         5        31 burntb…     2 no     
 8     68 Chirodzo 2016-11-16 00:00:00         8        52 burntb…     3 no     
 9    199 Chirodzo 2017-06-04 00:00:00         7        17 burntb…     2 yes    
10    200 Chirodzo 2017-06-04 00:00:00         8        20 burntb…     2    
# … with 6 more variables: affect_conflicts , liv_count ,
#   items_owned , no_meals , months_lack_food ,
#   instanceID , and abbreviated variable names ¹​respondent_wall_type,
#   ²​memb_assoc
9
provides
# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
29. For example, if we wanted to count the number of rows of data for each village, we would do:

# filters observations with "and" operator (comma)
# output dataframe satisfies ALL specified conditions
filter(interviews, village == "Chirodzo", 
                   rooms > 1, 
                   no_meals > 2)
3

# filters observations with "and" operator (comma)
# output dataframe satisfies ALL specified conditions
filter(interviews, village == "Chirodzo", 
                   rooms > 1, 
                   no_meals > 2)
4

Để thuận tiện,

# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
29 cung cấp đối số
# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
94 để nhận kết quả trong việc giảm lệnh:

# filters observations with "and" operator (comma)
# output dataframe satisfies ALL specified conditions
filter(interviews, village == "Chirodzo", 
                   rooms > 1, 
                   no_meals > 2)
5

# filters observations with "and" operator (comma)
# output dataframe satisfies ALL specified conditions
filter(interviews, village == "Chirodzo", 
                   rooms > 1, 
                   no_meals > 2)
6

Tập thể dục

Có bao nhiêu hộ gia đình trong cuộc khảo sát có trung bình hai bữa ăn mỗi ngày? Ba bữa ăn mỗi ngày? Có số lượng bữa ăn nào khác được đại diện không?

Dung dịch

# filters observations with "and" operator (comma)
# output dataframe satisfies ALL specified conditions
filter(interviews, village == "Chirodzo", 
                   rooms > 1, 
                   no_meals > 2)
7

# filters observations with "and" operator (comma)
# output dataframe satisfies ALL specified conditions
filter(interviews, village == "Chirodzo", 
                   rooms > 1, 
                   no_meals > 2)
8

Sử dụng

# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
26 và
# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
27 để tìm số trung bình, tối thiểu và số lượng tối đa thành viên hộ gia đình cho mỗi làng. Ngoài ra, thêm số lượng quan sát (gợi ý: xem
# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
97).

Dung dịch

# filters observations with "and" operator (comma)
# output dataframe satisfies ALL specified conditions
filter(interviews, village == "Chirodzo", 
                   rooms > 1, 
                   no_meals > 2)
9

# A tibble: 10 × 14
   key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                       
 1     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
 2     49 Chirodzo 2016-11-16 00:00:00         6        26 burntb…     2    
 3     52 Chirodzo 2016-11-16 00:00:00        11        15 burntb…     3 no     
 4     56 Chirodzo 2016-11-16 00:00:00        12        23 burntb…     2 yes    
 5     65 Chirodzo 2016-11-16 00:00:00         8        20 burntb…     3 no     
 6     66 Chirodzo 2016-11-16 00:00:00        10        37 burntb…     3 yes    
 7     67 Chirodzo 2016-11-16 00:00:00         5        31 burntb…     2 no     
 8     68 Chirodzo 2016-11-16 00:00:00         8        52 burntb…     3 no     
 9    199 Chirodzo 2017-06-04 00:00:00         7        17 burntb…     2 yes    
10    200 Chirodzo 2017-06-04 00:00:00         8        20 burntb…     2    
# … with 6 more variables: affect_conflicts , liv_count ,
#   items_owned , no_meals , months_lack_food ,
#   instanceID , and abbreviated variable names ¹​respondent_wall_type,
#   ²​memb_assoc
0

Các hộ gia đình lớn nhất được phỏng vấn mỗi tháng là gì?

Dung dịch

# A tibble: 10 × 14
   key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                       
 1     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
 2     49 Chirodzo 2016-11-16 00:00:00         6        26 burntb…     2    
 3     52 Chirodzo 2016-11-16 00:00:00        11        15 burntb…     3 no     
 4     56 Chirodzo 2016-11-16 00:00:00        12        23 burntb…     2 yes    
 5     65 Chirodzo 2016-11-16 00:00:00         8        20 burntb…     3 no     
 6     66 Chirodzo 2016-11-16 00:00:00        10        37 burntb…     3 yes    
 7     67 Chirodzo 2016-11-16 00:00:00         5        31 burntb…     2 no     
 8     68 Chirodzo 2016-11-16 00:00:00         8        52 burntb…     3 no     
 9    199 Chirodzo 2017-06-04 00:00:00         7        17 burntb…     2 yes    
10    200 Chirodzo 2017-06-04 00:00:00         8        20 burntb…     2    
# … with 6 more variables: affect_conflicts , liv_count ,
#   items_owned , no_meals , months_lack_food ,
#   instanceID , and abbreviated variable names ¹​respondent_wall_type,
#   ²​memb_assoc
1

# A tibble: 10 × 14
   key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                       
 1     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
 2     49 Chirodzo 2016-11-16 00:00:00         6        26 burntb…     2    
 3     52 Chirodzo 2016-11-16 00:00:00        11        15 burntb…     3 no     
 4     56 Chirodzo 2016-11-16 00:00:00        12        23 burntb…     2 yes    
 5     65 Chirodzo 2016-11-16 00:00:00         8        20 burntb…     3 no     
 6     66 Chirodzo 2016-11-16 00:00:00        10        37 burntb…     3 yes    
 7     67 Chirodzo 2016-11-16 00:00:00         5        31 burntb…     2 no     
 8     68 Chirodzo 2016-11-16 00:00:00         8        52 burntb…     3 no     
 9    199 Chirodzo 2017-06-04 00:00:00         7        17 burntb…     2 yes    
10    200 Chirodzo 2017-06-04 00:00:00         8        20 burntb…     2    
# … with 6 more variables: affect_conflicts , liv_count ,
#   items_owned , no_meals , months_lack_food ,
#   instanceID , and abbreviated variable names ¹​respondent_wall_type,
#   ²​memb_assoc
2

# A tibble: 10 × 14
   key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                       
 1     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
 2     49 Chirodzo 2016-11-16 00:00:00         6        26 burntb…     2    
 3     52 Chirodzo 2016-11-16 00:00:00        11        15 burntb…     3 no     
 4     56 Chirodzo 2016-11-16 00:00:00        12        23 burntb…     2 yes    
 5     65 Chirodzo 2016-11-16 00:00:00         8        20 burntb…     3 no     
 6     66 Chirodzo 2016-11-16 00:00:00        10        37 burntb…     3 yes    
 7     67 Chirodzo 2016-11-16 00:00:00         5        31 burntb…     2 no     
 8     68 Chirodzo 2016-11-16 00:00:00         8        52 burntb…     3 no     
 9    199 Chirodzo 2017-06-04 00:00:00         7        17 burntb…     2 yes    
10    200 Chirodzo 2017-06-04 00:00:00         8        20 burntb…     2    
# … with 6 more variables: affect_conflicts , liv_count ,
#   items_owned , no_meals , months_lack_food ,
#   instanceID , and abbreviated variable names ¹​respondent_wall_type,
#   ²​memb_assoc
3

# A tibble: 10 × 14
   key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                       
 1     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
 2     49 Chirodzo 2016-11-16 00:00:00         6        26 burntb…     2    
 3     52 Chirodzo 2016-11-16 00:00:00        11        15 burntb…     3 no     
 4     56 Chirodzo 2016-11-16 00:00:00        12        23 burntb…     2 yes    
 5     65 Chirodzo 2016-11-16 00:00:00         8        20 burntb…     3 no     
 6     66 Chirodzo 2016-11-16 00:00:00        10        37 burntb…     3 yes    
 7     67 Chirodzo 2016-11-16 00:00:00         5        31 burntb…     2 no     
 8     68 Chirodzo 2016-11-16 00:00:00         8        52 burntb…     3 no     
 9    199 Chirodzo 2017-06-04 00:00:00         7        17 burntb…     2 yes    
10    200 Chirodzo 2017-06-04 00:00:00         8        20 burntb…     2    
# … with 6 more variables: affect_conflicts , liv_count ,
#   items_owned , no_meals , months_lack_food ,
#   instanceID , and abbreviated variable names ¹​respondent_wall_type,
#   ²​memb_assoc
4

# A tibble: 10 × 14
   key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                       
 1     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
 2     49 Chirodzo 2016-11-16 00:00:00         6        26 burntb…     2    
 3     52 Chirodzo 2016-11-16 00:00:00        11        15 burntb…     3 no     
 4     56 Chirodzo 2016-11-16 00:00:00        12        23 burntb…     2 yes    
 5     65 Chirodzo 2016-11-16 00:00:00         8        20 burntb…     3 no     
 6     66 Chirodzo 2016-11-16 00:00:00        10        37 burntb…     3 yes    
 7     67 Chirodzo 2016-11-16 00:00:00         5        31 burntb…     2 no     
 8     68 Chirodzo 2016-11-16 00:00:00         8        52 burntb…     3 no     
 9    199 Chirodzo 2017-06-04 00:00:00         7        17 burntb…     2 yes    
10    200 Chirodzo 2017-06-04 00:00:00         8        20 burntb…     2    
# … with 6 more variables: affect_conflicts , liv_count ,
#   items_owned , no_meals , months_lack_food ,
#   instanceID , and abbreviated variable names ¹​respondent_wall_type,
#   ²​memb_assoc
5

# A tibble: 10 × 14
   key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                       
 1     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
 2     49 Chirodzo 2016-11-16 00:00:00         6        26 burntb…     2    
 3     52 Chirodzo 2016-11-16 00:00:00        11        15 burntb…     3 no     
 4     56 Chirodzo 2016-11-16 00:00:00        12        23 burntb…     2 yes    
 5     65 Chirodzo 2016-11-16 00:00:00         8        20 burntb…     3 no     
 6     66 Chirodzo 2016-11-16 00:00:00        10        37 burntb…     3 yes    
 7     67 Chirodzo 2016-11-16 00:00:00         5        31 burntb…     2 no     
 8     68 Chirodzo 2016-11-16 00:00:00         8        52 burntb…     3 no     
 9    199 Chirodzo 2017-06-04 00:00:00         7        17 burntb…     2 yes    
10    200 Chirodzo 2017-06-04 00:00:00         8        20 burntb…     2    
# … with 6 more variables: affect_conflicts , liv_count ,
#   items_owned , no_meals , months_lack_food ,
#   instanceID , and abbreviated variable names ¹​respondent_wall_type,
#   ²​memb_assoc
6

Định hình lại với pivot_wider () và pivot_longer ()

Về cơ bản, có ba quy tắc xác định một bộ dữ liệu của người Viking TIDY:

  1. Mỗi biến có cột riêng
  2. Mỗi quan sát có hàng riêng
  3. Mỗi giá trị phải có ô riêng của nó

Trong phần này, chúng tôi sẽ khám phá cách các quy tắc này được liên kết với các định dạng dữ liệu khác nhau, các nhà nghiên cứu thường quan tâm đến: rộng rãi và rộng. Hướng dẫn này sẽ giúp bạn chuyển đổi hiệu quả hình dạng dữ liệu của bạn bất kể định dạng gốc. Đầu tiên chúng tôi sẽ khám phá các phẩm chất của dữ liệu

# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
31 và cách chúng liên quan đến các loại định dạng dữ liệu khác nhau này.

Các định dạng dữ liệu dài và rộng

Trong dữ liệu

# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
31, mỗi hàng chứa các giá trị của các biến được liên kết với mỗi bản ghi được thu thập (mỗi cuộc phỏng vấn trong các làng), trong đó đã tuyên bố rằng
# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
00 đã được thêm vào để cung cấp một ID duy nhất cho mỗi quan sát và
# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
01 cũng vậy nhưng nó không thuận tiện để sử dụng.

Tuy nhiên, với một số kiểm tra, chúng tôi nhận thấy rằng có nhiều hơn một hàng trong bộ dữ liệu có cùng

# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
00 (như được thấy dưới đây). Tuy nhiên, các
# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
03 liên quan đến các
# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
00 trùng lặp này không giống nhau. Vì vậy, chúng ta nên nghĩ về
# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
03 là định danh duy nhất cho các quan sát!

# A tibble: 10 × 14
   key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                       
 1     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
 2     49 Chirodzo 2016-11-16 00:00:00         6        26 burntb…     2    
 3     52 Chirodzo 2016-11-16 00:00:00        11        15 burntb…     3 no     
 4     56 Chirodzo 2016-11-16 00:00:00        12        23 burntb…     2 yes    
 5     65 Chirodzo 2016-11-16 00:00:00         8        20 burntb…     3 no     
 6     66 Chirodzo 2016-11-16 00:00:00        10        37 burntb…     3 yes    
 7     67 Chirodzo 2016-11-16 00:00:00         5        31 burntb…     2 no     
 8     68 Chirodzo 2016-11-16 00:00:00         8        52 burntb…     3 no     
 9    199 Chirodzo 2017-06-04 00:00:00         7        17 burntb…     2 yes    
10    200 Chirodzo 2017-06-04 00:00:00         8        20 burntb…     2    
# … with 6 more variables: affect_conflicts , liv_count ,
#   items_owned , no_meals , months_lack_food ,
#   instanceID , and abbreviated variable names ¹​respondent_wall_type,
#   ²​memb_assoc
7

# A tibble: 10 × 14
   key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                       
 1     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
 2     49 Chirodzo 2016-11-16 00:00:00         6        26 burntb…     2    
 3     52 Chirodzo 2016-11-16 00:00:00        11        15 burntb…     3 no     
 4     56 Chirodzo 2016-11-16 00:00:00        12        23 burntb…     2 yes    
 5     65 Chirodzo 2016-11-16 00:00:00         8        20 burntb…     3 no     
 6     66 Chirodzo 2016-11-16 00:00:00        10        37 burntb…     3 yes    
 7     67 Chirodzo 2016-11-16 00:00:00         5        31 burntb…     2 no     
 8     68 Chirodzo 2016-11-16 00:00:00         8        52 burntb…     3 no     
 9    199 Chirodzo 2017-06-04 00:00:00         7        17 burntb…     2 yes    
10    200 Chirodzo 2017-06-04 00:00:00         8        20 burntb…     2    
# … with 6 more variables: affect_conflicts , liv_count ,
#   items_owned , no_meals , months_lack_food ,
#   instanceID , and abbreviated variable names ¹​respondent_wall_type,
#   ²​memb_assoc
8

Như đã thấy trong mã dưới đây, đối với mỗi ngày phỏng vấn ở mỗi làng, không có

# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
03 là như nhau. Do đó, định dạng này là cái được gọi là định dạng dữ liệu của Long Long, trong đó mỗi quan sát chỉ chiếm một hàng trong khung dữ liệu.

# A tibble: 10 × 14
   key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                       
 1     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
 2     49 Chirodzo 2016-11-16 00:00:00         6        26 burntb…     2    
 3     52 Chirodzo 2016-11-16 00:00:00        11        15 burntb…     3 no     
 4     56 Chirodzo 2016-11-16 00:00:00        12        23 burntb…     2 yes    
 5     65 Chirodzo 2016-11-16 00:00:00         8        20 burntb…     3 no     
 6     66 Chirodzo 2016-11-16 00:00:00        10        37 burntb…     3 yes    
 7     67 Chirodzo 2016-11-16 00:00:00         5        31 burntb…     2 no     
 8     68 Chirodzo 2016-11-16 00:00:00         8        52 burntb…     3 no     
 9    199 Chirodzo 2017-06-04 00:00:00         7        17 burntb…     2 yes    
10    200 Chirodzo 2017-06-04 00:00:00         8        20 burntb…     2    
# … with 6 more variables: affect_conflicts , liv_count ,
#   items_owned , no_meals , months_lack_food ,
#   instanceID , and abbreviated variable names ¹​respondent_wall_type,
#   ²​memb_assoc
9

# filters observations with "&" logical operator
# output dataframe satisfies ALL specified conditions
filter(interviews, village == "Chirodzo" & 
                   rooms > 1 & 
                   no_meals > 2)
0

Chúng tôi nhận thấy rằng bố cục hoặc định dạng của dữ liệu

# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
31 ở định dạng tuân thủ các quy tắc 1-3, trong đó

  • Mỗi cột là một biến
  • Mỗi hàng là một quan sát
  • Mỗi giá trị có ô riêng của nó

Đây được gọi là định dạng dữ liệu dài của người Viking. Nhưng, chúng tôi nhận thấy rằng mỗi cột đại diện cho một biến khác nhau. Trong định dạng dữ liệu dài nhất trên mạng, sẽ chỉ có ba cột, một cột cho biến ID, một cho biến quan sát và một cho giá trị quan sát (của biến đó). Định dạng dữ liệu này khá khó coi và khó làm việc, vì vậy bạn sẽ hiếm khi thấy nó được sử dụng.

Ngoài ra, trong một định dạng dữ liệu rộng rãi trên mạng, chúng tôi thấy các sửa đổi theo Quy tắc 1, trong đó mỗi cột không còn đại diện cho một biến duy nhất. Thay vào đó, các cột có thể biểu diễn các cấp/giá trị khác nhau của một biến. Ví dụ, trong một số dữ liệu bạn gặp phải, các nhà nghiên cứu có thể đã chọn cho mỗi ngày khảo sát là một cột khác nhau.

Chúng nghe có vẻ giống như bố cục dữ liệu khác nhau đáng kể, nhưng có một số công cụ giúp chuyển đổi giữa các bố cục này đơn giản hơn nhiều so với bạn nghĩ! GIF dưới đây cho thấy hai định dạng này liên quan đến nhau như thế nào và cho bạn ý tưởng về cách chúng ta có thể sử dụng R để chuyển từ định dạng này sang định dạng khác.

Hướng dẫn tidyr cheatsheet - cheatsheet gọn gàng
Bố cục DataFrame dài và rộng chủ yếu ảnh hưởng đến khả năng đọc. Bạn có thể thấy rằng trực quan mà bạn có thể thích định dạng trên phạm vi rộng, vì bạn có thể thấy thêm dữ liệu trên màn hình. Tuy nhiên, tất cả các chức năng R mà chúng tôi đã sử dụng cho đến nay mong đợi dữ liệu của bạn ở định dạng dữ liệu dài. Điều này là do định dạng dài có thể đọc được nhiều hơn và gần với định dạng của cơ sở dữ liệu.

Các câu hỏi đảm bảo các định dạng dữ liệu khác nhau

Trong các cuộc phỏng vấn, mỗi hàng chứa các giá trị của các biến liên quan đến mỗi bản ghi (đơn vị), các giá trị như làng của người trả lời, số lượng thành viên trong gia đình hoặc loại tường mà ngôi nhà của họ có. Định dạng này cho phép chúng tôi so sánh các cuộc khảo sát riêng lẻ, nhưng nếu chúng tôi muốn xem xét sự khác biệt trong các hộ gia đình được nhóm bởi các loại vật liệu xây dựng nhà ở khác nhau thì sao?

Để tạo điều kiện so sánh này, chúng tôi sẽ cần tạo một bảng mới trong đó mỗi hàng (đơn vị) bao gồm các giá trị của các biến liên quan đến vật liệu nhà ở (ví dụ:

# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
47). Về mặt thực tế, điều này có nghĩa là các giá trị của các vật liệu xây dựng tường trong
# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
47 (ví dụ: Muddaub, Burntbricks, xi măng, sunbricks) sẽ trở thành tên của các biến cột và các ô sẽ chứa các giá trị của
# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
68 hoặc
# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
71 của vật liệu đó.

Khi chúng tôi đã tạo ra bảng mới này, chúng tôi có thể khám phá mối quan hệ trong và giữa các ngôi làng. Điểm mấu chốt ở đây là chúng tôi vẫn đang tuân theo cấu trúc dữ liệu gọn gàng, nhưng chúng tôi đã định hình lại dữ liệu theo các quan sát quan tâm.reshaped the data according to the observations of interest.

Ngoài ra, nếu ngày phỏng vấn được trải rộng trên nhiều cột và chúng tôi quan tâm đến việc hình dung, trong mỗi làng, xung đột thủy lợi đã thay đổi theo thời gian như thế nào. Điều này sẽ yêu cầu ngày phỏng vấn được đưa vào một cột thay vì trải rộng trên nhiều cột. Do đó, chúng ta sẽ cần phải chuyển đổi tên cột thành các giá trị của một biến.

Chúng ta có thể thực hiện cả hai biến đổi này với hai hàm

# filters observations with "|" logical operator
# output dataframe satisfies AT LEAST ONE of the specified conditions
filter(interviews, village == "Chirodzo" | village == "Ruaca")
0,
# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
13 và
# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
14.

Xoay tròn rộng hơn

# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
13 có ba đối số chính:

  1. dữ liệu
  2. Biến cột tên_From có ​​giá trị sẽ trở thành tên cột mới.
  3. Biến cột value_from có ​​giá trị sẽ lấp đầy các biến cột mới.

Các đối số tiếp theo bao gồm

# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
16, nếu được đặt, điền vào các giá trị bị thiếu với giá trị được cung cấp.

Hãy để sử dụng

# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
13 để chuyển đổi các cuộc phỏng vấn để tạo các cột mới cho từng loại vật liệu xây dựng tường. Chúng tôi sẽ sử dụng toán tử ống như đã làm trước đây. Vì cả tham số
# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
18 và
# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
19 phải đến từ các giá trị cột, chúng tôi sẽ tạo một cột giả (chúng tôi sẽ đặt tên cho nó
# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
20) để giữ giá trị
# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
68, sau đó chúng tôi sẽ đặt vào cột thích hợp tương ứng với vật liệu xây dựng tường người trả lời đó. Khi sử dụng
# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
25 Nếu bạn đưa ra một giá trị duy nhất, nó sẽ được sử dụng cho tất cả các quan sát trong bộ dữ liệu.

Đối với mỗi hàng trong bảng mới được xoay của chúng tôi, chỉ một trong các cột loại tường mới được tạo sẽ có giá trị

# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
68, vì mỗi ngôi nhà chỉ có thể được làm bằng một loại tường. Giá trị mặc định mà
# A tibble: 88 × 14
   key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                       
 1      8 Chirodzo 2016-11-16 00:00:00        12        70 burntb…     3 yes    
 2      9 Chirodzo 2016-11-16 00:00:00         8         6 burntb…     1 no     
 3     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
 4     23 Ruaca    2016-11-21 00:00:00        10        20 burntb…     4    
 5     24 Ruaca    2016-11-21 00:00:00         6         4 burntb…     2 no     
 6     25 Ruaca    2016-11-21 00:00:00        11         6 burntb…     3 no     
 7     26 Ruaca    2016-11-21 00:00:00         3        20 burntb…     2 no     
 8     27 Ruaca    2016-11-21 00:00:00         7        36 burntb…     2    
 9     28 Ruaca    2016-11-21 00:00:00         2         2 muddaub     1 no     
10     29 Ruaca    2016-11-21 00:00:00         7        10 burntb…     2 yes    
# … with 78 more rows, 6 more variables: affect_conflicts ,
#   liv_count , items_owned , no_meals , months_lack_food ,
#   instanceID , and abbreviated variable names ¹​respondent_wall_type,
#   ²​memb_assoc
1 sử dụng để lấp đầy các loại tường khác là
# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
25.

Hướng dẫn tidyr cheatsheet - cheatsheet gọn gàng

Nếu thay vì giá trị mặc định là

# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
25, chúng tôi muốn các giá trị này là
# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
71, chúng tôi có thể chèn giá trị mặc định vào đối số
# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
16. Bằng cách bao gồm
# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
29 bên trong
# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
13, chúng ta có thể lấp đầy phần còn lại của các cột loại tường cho hàng đó với giá trị
# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
71.

# filters observations with "&" logical operator
# output dataframe satisfies ALL specified conditions
filter(interviews, village == "Chirodzo" & 
                   rooms > 1 & 
                   no_meals > 2)
1

Xem

# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
32 DataFrame và thông báo rằng không còn một cột có tiêu đề
# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
47. Điều này là do có một tham số mặc định trong
# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
13 làm giảm cột gốc. Các giá trị trong cột đó hiện đã trở thành các cột có tên
# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
35,
# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
36,
# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
37 và
# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
38. Bạn có thể sử dụng
# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
39 và
# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
40 để xem số lượng cột đã thay đổi giữa hai bộ dữ liệu.

Xoay vòng lâu hơn

Tình huống đối lập có thể xảy ra nếu chúng tôi được cung cấp dữ liệu dưới dạng

# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
32, trong đó các vật liệu xây dựng là tên cột, nhưng chúng tôi muốn coi chúng là giá trị của biến
# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
47 thay thế.

Trong tình huống này, chúng tôi đang thu thập các cột này biến chúng thành một cặp biến mới. Một biến bao gồm các tên cột là các giá trị và biến khác chứa các giá trị trong mỗi ô được liên kết trước đó với tên cột. Chúng tôi sẽ làm điều này trong hai bước để làm cho quá trình này rõ ràng hơn một chút.

# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
14 lấy bốn đối số chính:

  1. dữ liệu
  2. COLS là tên của các cột chúng tôi sử dụng để điền vào biến giá trị mới (hoặc giảm).
  3. Biến cột Tên_TO mà chúng tôi muốn tạo từ COL được cung cấp.
  4. Biến cột giá trị_to mà chúng tôi muốn tạo và điền vào các giá trị được liên kết với COL được cung cấp.

Để tạo lại DataFrame gốc của chúng tôi, chúng tôi sẽ sử dụng các mục sau:

  1. Dữ liệu -
    # filters observations where village name is "Chirodzo" 
    filter(interviews, village == "Chirodzo")
    
    32
  2. một danh sách các col (cột) sẽ được định hình lại; Chúng có thể được chỉ định bằng cách sử dụng
    # to select columns throughout the dataframe
    select(interviews, village, no_membrs, months_lack_food)
    # to do the same thing with subsetting
    interviews[c("village","no_membrs","months_lack_food")]
    # to select a series of connected columns
    select(interviews, village:respondent_wall_type)
    
    32 nếu các cột được định hình lại nằm trong một khu vực của DataFrame hoặc với lệnh vectơ (
    # filters observations where village name is "Chirodzo" 
    filter(interviews, village == "Chirodzo")
    
    46) nếu các cột được lan truyền khắp DataFrame.
  3. Cột tên_TO sẽ là một chuỗi ký tự của tên cột các cột này sẽ được thu gọn thành (Đáp lại_wall_type).
  4. Cột value_to sẽ là một chuỗi ký tự của tên của cột Các giá trị của các cột bị thu gọn sẽ được chèn vào (Hồi Wall_Type_Logical). Cột này sẽ được điền với các giá trị
    # to select columns throughout the dataframe
    select(interviews, village, no_membrs, months_lack_food)
    # to do the same thing with subsetting
    interviews[c("village","no_membrs","months_lack_food")]
    # to select a series of connected columns
    select(interviews, village:respondent_wall_type)
    
    68 hoặc
    # to select columns throughout the dataframe
    select(interviews, village, no_membrs, months_lack_food)
    # to do the same thing with subsetting
    interviews[c("village","no_membrs","months_lack_food")]
    # to select a series of connected columns
    select(interviews, village:respondent_wall_type)
    
    71.

# filters observations with "&" logical operator
# output dataframe satisfies ALL specified conditions
filter(interviews, village == "Chirodzo" & 
                   rooms > 1 & 
                   no_meals > 2)
2

Hướng dẫn tidyr cheatsheet - cheatsheet gọn gàng

Điều này tạo ra một khung dữ liệu với 524 hàng (4 hàng cho mỗi người trả lời phỏng vấn). Bốn hàng cho mỗi người trả lời chỉ khác nhau về giá trị của các cột trả lời của_wall_type và các cột Wall Wall_Type_Logical. Xem dữ liệu để xem cái này trông như thế nào.

Chỉ có một hàng cho mỗi người trả lời phỏng vấn là thông tin, chúng tôi biết rằng nếu các bức tường nhà được làm bằng từ Sun Sunrick, họ sẽ làm từ bất kỳ tài liệu nào khác. Do đó, sẽ có ý nghĩa khi lọc bộ dữ liệu của chúng tôi để chỉ giữ các giá trị trong đó

# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
20 là
# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
68. Bởi vì
# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
20 đã là
# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
68 hoặc
# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
71, khi chuyển tên cột sang
# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
24, nó sẽ tự động chỉ giữ các hàng trong đó cột này có giá trị
# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
68. Sau đó chúng ta có thể xóa cột
# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
20.

Chúng tôi thực hiện tất cả các bước này cùng nhau trong phần tiếp theo của mã:

# filters observations with "&" logical operator
# output dataframe satisfies ALL specified conditions
filter(interviews, village == "Chirodzo" & 
                   rooms > 1 & 
                   no_meals > 2)
3

Xem cả

# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
57 và
# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
32 và so sánh cấu trúc của chúng.

Áp dụng # filters observations where village name is "Chirodzo" filter(interviews, village == "Chirodzo") 13 để làm sạch dữ liệu của chúng tôi

Bây giờ chúng tôi đã tìm hiểu về

# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
14 và
# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
13, chúng tôi sẽ đưa các chức năng này vào sử dụng để khắc phục sự cố với cách cấu trúc dữ liệu của chúng tôi. Trong bài học bảng tính, chúng tôi đã học được rằng đó là cách thực hành tốt nhất để chỉ có một mẩu thông tin duy nhất trong mỗi ô của bảng tính của bạn. Trong bộ dữ liệu này, chúng tôi có một số cột chứa nhiều mẩu thông tin. Ví dụ: cột
# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
62 chứa thông tin về việc người trả lời của chúng tôi có sở hữu tủ lạnh, tivi, v.v. Để làm cho dữ liệu này dễ phân tích hơn hay không, chúng tôi sẽ phân chia cột này và tạo một cột mới cho mỗi mục. Mỗi ô trong cột đó sẽ là
# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
68 hoặc
# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
71 và sẽ cho biết liệu người trả lời phỏng vấn đó có sở hữu mục đó hay không (tương tự như những gì chúng tôi đã làm trước đây với
# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
65).

# filters observations with "&" logical operator
# output dataframe satisfies ALL specified conditions
filter(interviews, village == "Chirodzo" & 
                   rooms > 1 & 
                   no_meals > 2)
4

Có một vài khái niệm mới trong đoạn mã này, vì vậy hãy để Lừa đi qua từng dòng. Đầu tiên, chúng tôi tạo một đối tượng mới (

# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
66) dựa trên DataFrame
# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
31.

# filters observations with "&" logical operator
# output dataframe satisfies ALL specified conditions
filter(interviews, village == "Chirodzo" & 
                   rooms > 1 & 
                   no_meals > 2)
5

Sau đó, chúng tôi sử dụng hàm mới

# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
68 từ gói
# filters observations with "|" logical operator
# output dataframe satisfies AT LEAST ONE of the specified conditions
filter(interviews, village == "Chirodzo" | village == "Ruaca")
0 để tách các giá trị của
# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
62 dựa trên sự hiện diện của bán đại tá (
# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
71). Các giá trị của biến này là nhiều mục được phân tách bằng bán đại tá, vì vậy hành động này tạo ra một hàng cho mỗi mục được liệt kê trong sở hữu của hộ gia đình. Do đó, chúng tôi kết thúc với một phiên bản định dạng dài của bộ dữ liệu, với nhiều hàng cho mỗi người trả lời. Ví dụ, nếu một người trả lời có tivi và bảng điều khiển năng lượng mặt trời, thì người trả lời đó giờ sẽ có hai hàng, một hàng có truyền hình trực tuyến và một người còn lại với bảng điều khiển năng lượng mặt trời trong cột
# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
62.
# filters observations with "|" logical operator
# output dataframe satisfies AT LEAST ONE of the specified conditions
filter(interviews, village == "Chirodzo" | village == "Ruaca")
0
package to separate the values of
# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
62 based on the presence of semi-colons (
# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
71). The values of this variable were multiple items separated by semi-colons, so this action creates a row for each item listed in a household’s possession. Thus, we end up with a long format version of the dataset, with multiple rows for each respondent. For example, if a respondent has a television and a solar panel, that respondent will now have two rows, one with “television” and the other with “solar panel” in the
# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
62 column.

# filters observations with "&" logical operator
# output dataframe satisfies ALL specified conditions
filter(interviews, village == "Chirodzo" & 
                   rooms > 1 & 
                   no_meals > 2)
6

Bạn có thể nhận thấy rằng một trong các cột được gọi là

# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
73. Điều này là do một số người được hỏi không sở hữu bất kỳ mục nào trong danh sách người phỏng vấn. Chúng ta có thể sử dụng hàm
# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
74 để thay đổi các giá trị
# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
25 này thành một cái gì đó có ý nghĩa hơn. Hàm
# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
74 hy vọng bạn sẽ cung cấp cho nó một
# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
77 các cột mà bạn muốn thay thế các giá trị
# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
25 trong và giá trị mà bạn muốn thay thế
# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
25s. Điều này kết thúc như thế này:

# filters observations with "&" logical operator
# output dataframe satisfies ALL specified conditions
filter(interviews, village == "Chirodzo" & 
                   rooms > 1 & 
                   no_meals > 2)
7

Tiếp theo, chúng tôi tạo một biến mới có tên

# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
80, có một giá trị (
# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
68) cho mỗi hàng. Điều này có ý nghĩa, vì mỗi mặt hàng trong mỗi hàng được sở hữu bởi hộ gia đình đó. Chúng tôi đang xây dựng biến này để khi lan truyền
# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
62 trên nhiều cột, chúng tôi có thể lấp đầy các giá trị của các cột đó bằng các giá trị logic mô tả liệu hộ gia đình đã làm (
# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
68) hoặc didn (
# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
71) sở hữu mục cụ thể đó.

# filters observations with "&" logical operator
# output dataframe satisfies ALL specified conditions
filter(interviews, village == "Chirodzo" & 
                   rooms > 1 & 
                   no_meals > 2)
8

Cuối cùng, chúng tôi sử dụng

# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
13 để chuyển từ định dạng dài sang định dạng rộng. Điều này tạo ra một cột mới cho từng giá trị duy nhất trong cột
# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
62 và lấp đầy các cột đó bằng các giá trị của
# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
80. Chúng tôi cũng tuyên bố rằng đối với các mục bị thiếu, chúng tôi muốn lấp đầy các ô đó với giá trị
# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
71 thay vì
# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
25.

# filters observations with "&" logical operator
# output dataframe satisfies ALL specified conditions
filter(interviews, village == "Chirodzo" & 
                   rooms > 1 & 
                   no_meals > 2)
9

Xem

# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
66 DataFrame. Nó nên có 131 hàng (cùng một số hàng bạn có ban đầu), nhưng các cột bổ sung cho mỗi mục. Có bao nhiêu cột đã được thêm vào?

Định dạng dữ liệu này cho phép chúng tôi làm những điều thú vị, như tạo bảng hiển thị số lượng người trả lời ở mỗi làng sở hữu một mục cụ thể:

# A tibble: 10 × 14
   key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                       
 1     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
 2     49 Chirodzo 2016-11-16 00:00:00         6        26 burntb…     2    
 3     52 Chirodzo 2016-11-16 00:00:00        11        15 burntb…     3 no     
 4     56 Chirodzo 2016-11-16 00:00:00        12        23 burntb…     2 yes    
 5     65 Chirodzo 2016-11-16 00:00:00         8        20 burntb…     3 no     
 6     66 Chirodzo 2016-11-16 00:00:00        10        37 burntb…     3 yes    
 7     67 Chirodzo 2016-11-16 00:00:00         5        31 burntb…     2 no     
 8     68 Chirodzo 2016-11-16 00:00:00         8        52 burntb…     3 no     
 9    199 Chirodzo 2017-06-04 00:00:00         7        17 burntb…     2 yes    
10    200 Chirodzo 2017-06-04 00:00:00         8        20 burntb…     2    
# … with 6 more variables: affect_conflicts , liv_count ,
#   items_owned , no_meals , months_lack_food ,
#   instanceID , and abbreviated variable names ¹​respondent_wall_type,
#   ²​memb_assoc
0

# A tibble: 10 × 14
   key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                       
 1     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
 2     49 Chirodzo 2016-11-16 00:00:00         6        26 burntb…     2    
 3     52 Chirodzo 2016-11-16 00:00:00        11        15 burntb…     3 no     
 4     56 Chirodzo 2016-11-16 00:00:00        12        23 burntb…     2 yes    
 5     65 Chirodzo 2016-11-16 00:00:00         8        20 burntb…     3 no     
 6     66 Chirodzo 2016-11-16 00:00:00        10        37 burntb…     3 yes    
 7     67 Chirodzo 2016-11-16 00:00:00         5        31 burntb…     2 no     
 8     68 Chirodzo 2016-11-16 00:00:00         8        52 burntb…     3 no     
 9    199 Chirodzo 2017-06-04 00:00:00         7        17 burntb…     2 yes    
10    200 Chirodzo 2017-06-04 00:00:00         8        20 burntb…     2    
# … with 6 more variables: affect_conflicts , liv_count ,
#   items_owned , no_meals , months_lack_food ,
#   instanceID , and abbreviated variable names ¹​respondent_wall_type,
#   ²​memb_assoc
1

Hoặc dưới đây, chúng tôi tính toán số lượng mục trung bình từ danh sách thuộc sở hữu của người trả lời ở mỗi làng. Mã này sử dụng hàm

# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
91 để đếm số giá trị
# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
68 trong các cột
# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
93 đến
# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
94 cho mỗi hàng, do đó tên của nó. Lưu ý rằng chúng tôi đã thay thế các giá trị
# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
25 bằng giá trị
# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
96, vì vậy chúng tôi phải loại trừ giá trị này trong tập hợp. Sau đó, chúng tôi nhóm dữ liệu theo làng và tính toán số lượng mặt hàng trung bình, vì vậy mỗi trung bình được nhóm lại bởi làng.

# A tibble: 10 × 14
   key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                       
 1     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
 2     49 Chirodzo 2016-11-16 00:00:00         6        26 burntb…     2    
 3     52 Chirodzo 2016-11-16 00:00:00        11        15 burntb…     3 no     
 4     56 Chirodzo 2016-11-16 00:00:00        12        23 burntb…     2 yes    
 5     65 Chirodzo 2016-11-16 00:00:00         8        20 burntb…     3 no     
 6     66 Chirodzo 2016-11-16 00:00:00        10        37 burntb…     3 yes    
 7     67 Chirodzo 2016-11-16 00:00:00         5        31 burntb…     2 no     
 8     68 Chirodzo 2016-11-16 00:00:00         8        52 burntb…     3 no     
 9    199 Chirodzo 2017-06-04 00:00:00         7        17 burntb…     2 yes    
10    200 Chirodzo 2017-06-04 00:00:00         8        20 burntb…     2    
# … with 6 more variables: affect_conflicts , liv_count ,
#   items_owned , no_meals , months_lack_food ,
#   instanceID , and abbreviated variable names ¹​respondent_wall_type,
#   ²​memb_assoc
2

# A tibble: 10 × 14
   key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                       
 1     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
 2     49 Chirodzo 2016-11-16 00:00:00         6        26 burntb…     2    
 3     52 Chirodzo 2016-11-16 00:00:00        11        15 burntb…     3 no     
 4     56 Chirodzo 2016-11-16 00:00:00        12        23 burntb…     2 yes    
 5     65 Chirodzo 2016-11-16 00:00:00         8        20 burntb…     3 no     
 6     66 Chirodzo 2016-11-16 00:00:00        10        37 burntb…     3 yes    
 7     67 Chirodzo 2016-11-16 00:00:00         5        31 burntb…     2 no     
 8     68 Chirodzo 2016-11-16 00:00:00         8        52 burntb…     3 no     
 9    199 Chirodzo 2017-06-04 00:00:00         7        17 burntb…     2 yes    
10    200 Chirodzo 2017-06-04 00:00:00         8        20 burntb…     2    
# … with 6 more variables: affect_conflicts , liv_count ,
#   items_owned , no_meals , months_lack_food ,
#   instanceID , and abbreviated variable names ¹​respondent_wall_type,
#   ²​memb_assoc
3

Tập thể dục

  1. Tạo một DataFrame mới (có tên
    # filters observations where village name is "Chirodzo" 
    filter(interviews, village == "Chirodzo")
    
    97) có một cột cho mỗi tháng và hồ sơ
    # to select columns throughout the dataframe
    select(interviews, village, no_membrs, months_lack_food)
    # to do the same thing with subsetting
    interviews[c("village","no_membrs","months_lack_food")]
    # to select a series of connected columns
    select(interviews, village:respondent_wall_type)
    
    68 hoặc
    # to select columns throughout the dataframe
    select(interviews, village, no_membrs, months_lack_food)
    # to do the same thing with subsetting
    interviews[c("village","no_membrs","months_lack_food")]
    # to select a series of connected columns
    select(interviews, village:respondent_wall_type)
    
    71 cho việc mỗi người trả lời phỏng vấn có thiếu thực phẩm trong tháng đó hay không.

Dung dịch

# A tibble: 10 × 14
   key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                       
 1     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
 2     49 Chirodzo 2016-11-16 00:00:00         6        26 burntb…     2    
 3     52 Chirodzo 2016-11-16 00:00:00        11        15 burntb…     3 no     
 4     56 Chirodzo 2016-11-16 00:00:00        12        23 burntb…     2 yes    
 5     65 Chirodzo 2016-11-16 00:00:00         8        20 burntb…     3 no     
 6     66 Chirodzo 2016-11-16 00:00:00        10        37 burntb…     3 yes    
 7     67 Chirodzo 2016-11-16 00:00:00         5        31 burntb…     2 no     
 8     68 Chirodzo 2016-11-16 00:00:00         8        52 burntb…     3 no     
 9    199 Chirodzo 2017-06-04 00:00:00         7        17 burntb…     2 yes    
10    200 Chirodzo 2017-06-04 00:00:00         8        20 burntb…     2    
# … with 6 more variables: affect_conflicts , liv_count ,
#   items_owned , no_meals , months_lack_food ,
#   instanceID , and abbreviated variable names ¹​respondent_wall_type,
#   ²​memb_assoc
4

  1. Có bao nhiêu tháng (trung bình) là người trả lời không có thức ăn nếu họ thuộc về một hiệp hội thủy lợi? Thế còn nếu họ đã làm?

Dung dịch

# A tibble: 10 × 14
   key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                       
 1     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
 2     49 Chirodzo 2016-11-16 00:00:00         6        26 burntb…     2    
 3     52 Chirodzo 2016-11-16 00:00:00        11        15 burntb…     3 no     
 4     56 Chirodzo 2016-11-16 00:00:00        12        23 burntb…     2 yes    
 5     65 Chirodzo 2016-11-16 00:00:00         8        20 burntb…     3 no     
 6     66 Chirodzo 2016-11-16 00:00:00        10        37 burntb…     3 yes    
 7     67 Chirodzo 2016-11-16 00:00:00         5        31 burntb…     2 no     
 8     68 Chirodzo 2016-11-16 00:00:00         8        52 burntb…     3 no     
 9    199 Chirodzo 2017-06-04 00:00:00         7        17 burntb…     2 yes    
10    200 Chirodzo 2017-06-04 00:00:00         8        20 burntb…     2    
# … with 6 more variables: affect_conflicts , liv_count ,
#   items_owned , no_meals , months_lack_food ,
#   instanceID , and abbreviated variable names ¹​respondent_wall_type,
#   ²​memb_assoc
5

# A tibble: 10 × 14
   key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                       
 1     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
 2     49 Chirodzo 2016-11-16 00:00:00         6        26 burntb…     2    
 3     52 Chirodzo 2016-11-16 00:00:00        11        15 burntb…     3 no     
 4     56 Chirodzo 2016-11-16 00:00:00        12        23 burntb…     2 yes    
 5     65 Chirodzo 2016-11-16 00:00:00         8        20 burntb…     3 no     
 6     66 Chirodzo 2016-11-16 00:00:00        10        37 burntb…     3 yes    
 7     67 Chirodzo 2016-11-16 00:00:00         5        31 burntb…     2 no     
 8     68 Chirodzo 2016-11-16 00:00:00         8        52 burntb…     3 no     
 9    199 Chirodzo 2017-06-04 00:00:00         7        17 burntb…     2 yes    
10    200 Chirodzo 2017-06-04 00:00:00         8        20 burntb…     2    
# … with 6 more variables: affect_conflicts , liv_count ,
#   items_owned , no_meals , months_lack_food ,
#   instanceID , and abbreviated variable names ¹​respondent_wall_type,
#   ²​memb_assoc
6

Có bao nhiêu tháng (trung bình) là người trả lời không có thức ăn nếu họ thuộc về một hiệp hội thủy lợi? Thế còn nếu họ đã làm?

Xuất dữ liệu

# A tibble: 10 × 14
   key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                       
 1     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
 2     49 Chirodzo 2016-11-16 00:00:00         6        26 burntb…     2    
 3     52 Chirodzo 2016-11-16 00:00:00        11        15 burntb…     3 no     
 4     56 Chirodzo 2016-11-16 00:00:00        12        23 burntb…     2 yes    
 5     65 Chirodzo 2016-11-16 00:00:00         8        20 burntb…     3 no     
 6     66 Chirodzo 2016-11-16 00:00:00        10        37 burntb…     3 yes    
 7     67 Chirodzo 2016-11-16 00:00:00         5        31 burntb…     2 no     
 8     68 Chirodzo 2016-11-16 00:00:00         8        52 burntb…     3 no     
 9    199 Chirodzo 2017-06-04 00:00:00         7        17 burntb…     2 yes    
10    200 Chirodzo 2017-06-04 00:00:00         8        20 burntb…     2    
# … with 6 more variables: affect_conflicts , liv_count ,
#   items_owned , no_meals , months_lack_food ,
#   instanceID , and abbreviated variable names ¹​respondent_wall_type,
#   ²​memb_assoc
9 to extract information from or summarize your raw data, you may want to export these new data sets to share them with your collaborators or for archival.

Bây giờ bạn đã học được cách sử dụng

# A tibble: 10 × 14
   key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                       
 1     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
 2     49 Chirodzo 2016-11-16 00:00:00         6        26 burntb…     2    
 3     52 Chirodzo 2016-11-16 00:00:00        11        15 burntb…     3 no     
 4     56 Chirodzo 2016-11-16 00:00:00        12        23 burntb…     2 yes    
 5     65 Chirodzo 2016-11-16 00:00:00         8        20 burntb…     3 no     
 6     66 Chirodzo 2016-11-16 00:00:00        10        37 burntb…     3 yes    
 7     67 Chirodzo 2016-11-16 00:00:00         5        31 burntb…     2 no     
 8     68 Chirodzo 2016-11-16 00:00:00         8        52 burntb…     3 no     
 9    199 Chirodzo 2017-06-04 00:00:00         7        17 burntb…     2 yes    
10    200 Chirodzo 2017-06-04 00:00:00         8        20 burntb…     2    
# … with 6 more variables: affect_conflicts , liv_count ,
#   items_owned , no_meals , months_lack_food ,
#   instanceID , and abbreviated variable names ¹​respondent_wall_type,
#   ²​memb_assoc
9 để trích xuất thông tin từ hoặc tóm tắt dữ liệu thô của bạn, bạn có thể muốn xuất các bộ dữ liệu mới này để chia sẻ chúng với cộng tác viên của bạn hoặc để lưu trữ.

Tương tự như hàm

# A tibble: 39 × 14
   key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                       
 1      8 Chirodzo 2016-11-16 00:00:00        12        70 burntb…     3 yes    
 2      9 Chirodzo 2016-11-16 00:00:00         8         6 burntb…     1 no     
 3     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
 4     34 Chirodzo 2016-11-17 00:00:00         8        18 burntb…     3 yes    
 5     35 Chirodzo 2016-11-17 00:00:00         5        45 muddaub     1 yes    
 6     36 Chirodzo 2016-11-17 00:00:00         6        23 sunbri…     1 yes    
 7     37 Chirodzo 2016-11-17 00:00:00         3         8 burntb…     1    
 8     43 Chirodzo 2016-11-17 00:00:00         7        29 muddaub     1 no     
 9     44 Chirodzo 2016-11-17 00:00:00         2         6 muddaub     1    
10     45 Chirodzo 2016-11-17 00:00:00         9         7 muddaub     1 no     
# … with 29 more rows, 6 more variables: affect_conflicts ,
#   liv_count , items_owned , no_meals , months_lack_food ,
#   instanceID , and abbreviated variable names ¹​respondent_wall_type,
#   ²​memb_assoc
01 được sử dụng để đọc các tệp CSV vào R, có chức năng
# A tibble: 39 × 14
   key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                       
 1      8 Chirodzo 2016-11-16 00:00:00        12        70 burntb…     3 yes    
 2      9 Chirodzo 2016-11-16 00:00:00         8         6 burntb…     1 no     
 3     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
 4     34 Chirodzo 2016-11-17 00:00:00         8        18 burntb…     3 yes    
 5     35 Chirodzo 2016-11-17 00:00:00         5        45 muddaub     1 yes    
 6     36 Chirodzo 2016-11-17 00:00:00         6        23 sunbri…     1 yes    
 7     37 Chirodzo 2016-11-17 00:00:00         3         8 burntb…     1    
 8     43 Chirodzo 2016-11-17 00:00:00         7        29 muddaub     1 no     
 9     44 Chirodzo 2016-11-17 00:00:00         2         6 muddaub     1    
10     45 Chirodzo 2016-11-17 00:00:00         9         7 muddaub     1 no     
# … with 29 more rows, 6 more variables: affect_conflicts ,
#   liv_count , items_owned , no_meals , months_lack_food ,
#   instanceID , and abbreviated variable names ¹​respondent_wall_type,
#   ²​memb_assoc
02 tạo các tệp CSV từ DataFrames.

Trước khi sử dụng

# A tibble: 39 × 14
   key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                       
 1      8 Chirodzo 2016-11-16 00:00:00        12        70 burntb…     3 yes    
 2      9 Chirodzo 2016-11-16 00:00:00         8         6 burntb…     1 no     
 3     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
 4     34 Chirodzo 2016-11-17 00:00:00         8        18 burntb…     3 yes    
 5     35 Chirodzo 2016-11-17 00:00:00         5        45 muddaub     1 yes    
 6     36 Chirodzo 2016-11-17 00:00:00         6        23 sunbri…     1 yes    
 7     37 Chirodzo 2016-11-17 00:00:00         3         8 burntb…     1    
 8     43 Chirodzo 2016-11-17 00:00:00         7        29 muddaub     1 no     
 9     44 Chirodzo 2016-11-17 00:00:00         2         6 muddaub     1    
10     45 Chirodzo 2016-11-17 00:00:00         9         7 muddaub     1 no     
# … with 29 more rows, 6 more variables: affect_conflicts ,
#   liv_count , items_owned , no_meals , months_lack_food ,
#   instanceID , and abbreviated variable names ¹​respondent_wall_type,
#   ²​memb_assoc
02, chúng tôi sẽ tạo một thư mục mới,
# A tibble: 39 × 14
   key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                       
 1      8 Chirodzo 2016-11-16 00:00:00        12        70 burntb…     3 yes    
 2      9 Chirodzo 2016-11-16 00:00:00         8         6 burntb…     1 no     
 3     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
 4     34 Chirodzo 2016-11-17 00:00:00         8        18 burntb…     3 yes    
 5     35 Chirodzo 2016-11-17 00:00:00         5        45 muddaub     1 yes    
 6     36 Chirodzo 2016-11-17 00:00:00         6        23 sunbri…     1 yes    
 7     37 Chirodzo 2016-11-17 00:00:00         3         8 burntb…     1    
 8     43 Chirodzo 2016-11-17 00:00:00         7        29 muddaub     1 no     
 9     44 Chirodzo 2016-11-17 00:00:00         2         6 muddaub     1    
10     45 Chirodzo 2016-11-17 00:00:00         9         7 muddaub     1 no     
# … with 29 more rows, 6 more variables: affect_conflicts ,
#   liv_count , items_owned , no_meals , months_lack_food ,
#   instanceID , and abbreviated variable names ¹​respondent_wall_type,
#   ²​memb_assoc
04, trong thư mục làm việc của chúng tôi sẽ lưu trữ bộ dữ liệu được tạo này. Chúng tôi không muốn viết các bộ dữ liệu được tạo trong cùng thư mục với dữ liệu thô của chúng tôi. Nó thực hành tốt để giữ chúng riêng biệt. Thư mục
# A tibble: 39 × 14
   key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                       
 1      8 Chirodzo 2016-11-16 00:00:00        12        70 burntb…     3 yes    
 2      9 Chirodzo 2016-11-16 00:00:00         8         6 burntb…     1 no     
 3     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
 4     34 Chirodzo 2016-11-17 00:00:00         8        18 burntb…     3 yes    
 5     35 Chirodzo 2016-11-17 00:00:00         5        45 muddaub     1 yes    
 6     36 Chirodzo 2016-11-17 00:00:00         6        23 sunbri…     1 yes    
 7     37 Chirodzo 2016-11-17 00:00:00         3         8 burntb…     1    
 8     43 Chirodzo 2016-11-17 00:00:00         7        29 muddaub     1 no     
 9     44 Chirodzo 2016-11-17 00:00:00         2         6 muddaub     1    
10     45 Chirodzo 2016-11-17 00:00:00         9         7 muddaub     1 no     
# … with 29 more rows, 6 more variables: affect_conflicts ,
#   liv_count , items_owned , no_meals , months_lack_food ,
#   instanceID , and abbreviated variable names ¹​respondent_wall_type,
#   ²​memb_assoc
05 chỉ nên chứa dữ liệu thô, không thay đổi và nên được để lại một mình để đảm bảo rằng chúng tôi không xóa hoặc sửa đổi nó. Ngược lại, tập lệnh của chúng tôi sẽ tạo nội dung của thư mục
# A tibble: 39 × 14
   key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                       
 1      8 Chirodzo 2016-11-16 00:00:00        12        70 burntb…     3 yes    
 2      9 Chirodzo 2016-11-16 00:00:00         8         6 burntb…     1 no     
 3     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
 4     34 Chirodzo 2016-11-17 00:00:00         8        18 burntb…     3 yes    
 5     35 Chirodzo 2016-11-17 00:00:00         5        45 muddaub     1 yes    
 6     36 Chirodzo 2016-11-17 00:00:00         6        23 sunbri…     1 yes    
 7     37 Chirodzo 2016-11-17 00:00:00         3         8 burntb…     1    
 8     43 Chirodzo 2016-11-17 00:00:00         7        29 muddaub     1 no     
 9     44 Chirodzo 2016-11-17 00:00:00         2         6 muddaub     1    
10     45 Chirodzo 2016-11-17 00:00:00         9         7 muddaub     1 no     
# … with 29 more rows, 6 more variables: affect_conflicts ,
#   liv_count , items_owned , no_meals , months_lack_food ,
#   instanceID , and abbreviated variable names ¹​respondent_wall_type,
#   ²​memb_assoc
04, vì vậy ngay cả khi các tệp mà nó chứa bị xóa, chúng tôi luôn có thể tạo lại chúng.

# A tibble: 10 × 14
   key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                       
 1     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
 2     49 Chirodzo 2016-11-16 00:00:00         6        26 burntb…     2    
 3     52 Chirodzo 2016-11-16 00:00:00        11        15 burntb…     3 no     
 4     56 Chirodzo 2016-11-16 00:00:00        12        23 burntb…     2 yes    
 5     65 Chirodzo 2016-11-16 00:00:00         8        20 burntb…     3 no     
 6     66 Chirodzo 2016-11-16 00:00:00        10        37 burntb…     3 yes    
 7     67 Chirodzo 2016-11-16 00:00:00         5        31 burntb…     2 no     
 8     68 Chirodzo 2016-11-16 00:00:00         8        52 burntb…     3 no     
 9    199 Chirodzo 2017-06-04 00:00:00         7        17 burntb…     2 yes    
10    200 Chirodzo 2017-06-04 00:00:00         8        20 burntb…     2    
# … with 6 more variables: affect_conflicts , liv_count ,
#   items_owned , no_meals , months_lack_food ,
#   instanceID , and abbreviated variable names ¹​respondent_wall_type,
#   ²​memb_assoc
7

Để chuẩn bị cho bài học tiếp theo của chúng tôi về sơ đồ, chúng tôi sẽ tạo một phiên bản của bộ dữ liệu trong đó mỗi cột chỉ bao gồm một giá trị dữ liệu. Để làm điều này, chúng tôi sẽ sử dụng

# A tibble: 88 × 14
   key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                       
 1      8 Chirodzo 2016-11-16 00:00:00        12        70 burntb…     3 yes    
 2      9 Chirodzo 2016-11-16 00:00:00         8         6 burntb…     1 no     
 3     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
 4     23 Ruaca    2016-11-21 00:00:00        10        20 burntb…     4    
 5     24 Ruaca    2016-11-21 00:00:00         6         4 burntb…     2 no     
 6     25 Ruaca    2016-11-21 00:00:00        11         6 burntb…     3 no     
 7     26 Ruaca    2016-11-21 00:00:00         3        20 burntb…     2 no     
 8     27 Ruaca    2016-11-21 00:00:00         7        36 burntb…     2    
 9     28 Ruaca    2016-11-21 00:00:00         2         2 muddaub     1 no     
10     29 Ruaca    2016-11-21 00:00:00         7        10 burntb…     2 yes    
# … with 78 more rows, 6 more variables: affect_conflicts ,
#   liv_count , items_owned , no_meals , months_lack_food ,
#   instanceID , and abbreviated variable names ¹​respondent_wall_type,
#   ²​memb_assoc
1 để mở rộng các cột
# A tibble: 39 × 14
   key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                       
 1      8 Chirodzo 2016-11-16 00:00:00        12        70 burntb…     3 yes    
 2      9 Chirodzo 2016-11-16 00:00:00         8         6 burntb…     1 no     
 3     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
 4     34 Chirodzo 2016-11-17 00:00:00         8        18 burntb…     3 yes    
 5     35 Chirodzo 2016-11-17 00:00:00         5        45 muddaub     1 yes    
 6     36 Chirodzo 2016-11-17 00:00:00         6        23 sunbri…     1 yes    
 7     37 Chirodzo 2016-11-17 00:00:00         3         8 burntb…     1    
 8     43 Chirodzo 2016-11-17 00:00:00         7        29 muddaub     1 no     
 9     44 Chirodzo 2016-11-17 00:00:00         2         6 muddaub     1    
10     45 Chirodzo 2016-11-17 00:00:00         9         7 muddaub     1 no     
# … with 29 more rows, 6 more variables: affect_conflicts ,
#   liv_count , items_owned , no_meals , months_lack_food ,
#   instanceID , and abbreviated variable names ¹​respondent_wall_type,
#   ²​memb_assoc
08 và
# filters observations where village name is "Chirodzo" 
filter(interviews, village == "Chirodzo")
62. Chúng tôi cũng sẽ tạo ra một vài cột tóm tắt.

# A tibble: 10 × 14
   key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                       
 1     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
 2     49 Chirodzo 2016-11-16 00:00:00         6        26 burntb…     2    
 3     52 Chirodzo 2016-11-16 00:00:00        11        15 burntb…     3 no     
 4     56 Chirodzo 2016-11-16 00:00:00        12        23 burntb…     2 yes    
 5     65 Chirodzo 2016-11-16 00:00:00         8        20 burntb…     3 no     
 6     66 Chirodzo 2016-11-16 00:00:00        10        37 burntb…     3 yes    
 7     67 Chirodzo 2016-11-16 00:00:00         5        31 burntb…     2 no     
 8     68 Chirodzo 2016-11-16 00:00:00         8        52 burntb…     3 no     
 9    199 Chirodzo 2017-06-04 00:00:00         7        17 burntb…     2 yes    
10    200 Chirodzo 2017-06-04 00:00:00         8        20 burntb…     2    
# … with 6 more variables: affect_conflicts , liv_count ,
#   items_owned , no_meals , months_lack_food ,
#   instanceID , and abbreviated variable names ¹​respondent_wall_type,
#   ²​memb_assoc
8

Bây giờ chúng tôi có thể lưu khung dữ liệu này vào thư mục # A tibble: 39 × 14 key_ID village interview_date no_membrs years_liv respo…¹ rooms memb_…² 1 8 Chirodzo 2016-11-16 00:00:00 12 70 burntb… 3 yes 2 9 Chirodzo 2016-11-16 00:00:00 8 6 burntb… 1 no 3 10 Chirodzo 2016-12-16 00:00:00 12 23 burntb… 5 no 4 34 Chirodzo 2016-11-17 00:00:00 8 18 burntb… 3 yes 5 35 Chirodzo 2016-11-17 00:00:00 5 45 muddaub 1 yes 6 36 Chirodzo 2016-11-17 00:00:00 6 23 sunbri… 1 yes 7 37 Chirodzo 2016-11-17 00:00:00 3 8 burntb… 1 8 43 Chirodzo 2016-11-17 00:00:00 7 29 muddaub 1 no 9 44 Chirodzo 2016-11-17 00:00:00 2 6 muddaub 1 10 45 Chirodzo 2016-11-17 00:00:00 9 7 muddaub 1 no # … with 29 more rows, 6 more variables: affect_conflicts , # liv_count , items_owned , no_meals , months_lack_food , # instanceID , and abbreviated variable names ¹​respondent_wall_type, # ²​memb_assoc 04 của chúng tôi.

  • Những điểm chính

  • Sử dụng gói

    # A tibble: 10 × 14
       key_ID village  interview_date      no_membrs years_liv respo…¹ rooms memb_…²
                                           
     1     10 Chirodzo 2016-12-16 00:00:00        12        23 burntb…     5 no     
     2     49 Chirodzo 2016-11-16 00:00:00         6        26 burntb…     2    
     3     52 Chirodzo 2016-11-16 00:00:00        11        15 burntb…     3 no     
     4     56 Chirodzo 2016-11-16 00:00:00        12        23 burntb…     2 yes    
     5     65 Chirodzo 2016-11-16 00:00:00         8        20 burntb…     3 no     
     6     66 Chirodzo 2016-11-16 00:00:00        10        37 burntb…     3 yes    
     7     67 Chirodzo 2016-11-16 00:00:00         5        31 burntb…     2 no     
     8     68 Chirodzo 2016-11-16 00:00:00         8        52 burntb…     3 no     
     9    199 Chirodzo 2017-06-04 00:00:00         7        17 burntb…     2 yes    
    10    200 Chirodzo 2017-06-04 00:00:00         8        20 burntb…     2    
    # … with 6 more variables: affect_conflicts , liv_count ,
    #   items_owned , no_meals , months_lack_food ,
    #   instanceID , and abbreviated variable names ¹​respondent_wall_type,
    #   ²​memb_assoc
    
    9 để thao tác với DataFrames.

  • Sử dụng

    # to select columns throughout the dataframe
    select(interviews, village, no_membrs, months_lack_food)
    # to do the same thing with subsetting
    interviews[c("village","no_membrs","months_lack_food")]
    # to select a series of connected columns
    select(interviews, village:respondent_wall_type)
    
    23 để chọn các biến từ DataFrame.

  • Sử dụng

    # to select columns throughout the dataframe
    select(interviews, village, no_membrs, months_lack_food)
    # to do the same thing with subsetting
    interviews[c("village","no_membrs","months_lack_food")]
    # to select a series of connected columns
    select(interviews, village:respondent_wall_type)
    
    24 để chọn dữ liệu dựa trên các giá trị.

  • Sử dụng

    # to select columns throughout the dataframe
    select(interviews, village, no_membrs, months_lack_food)
    # to do the same thing with subsetting
    interviews[c("village","no_membrs","months_lack_food")]
    # to select a series of connected columns
    select(interviews, village:respondent_wall_type)
    
    26 và
    # to select columns throughout the dataframe
    select(interviews, village, no_membrs, months_lack_food)
    # to do the same thing with subsetting
    interviews[c("village","no_membrs","months_lack_food")]
    # to select a series of connected columns
    select(interviews, village:respondent_wall_type)
    
    27 để làm việc với các tập hợp dữ liệu.

  • Sử dụng

    # to select columns throughout the dataframe
    select(interviews, village, no_membrs, months_lack_food)
    # to do the same thing with subsetting
    interviews[c("village","no_membrs","months_lack_food")]
    # to select a series of connected columns
    select(interviews, village:respondent_wall_type)
    
    25 để tạo các biến mới.

  • Sử dụng gói

    # filters observations with "|" logical operator
    # output dataframe satisfies AT LEAST ONE of the specified conditions
    filter(interviews, village == "Chirodzo" | village == "Ruaca")
    
    0 để thay đổi bố cục của DataFrames.

  • Sử dụng

    # filters observations where village name is "Chirodzo" 
    filter(interviews, village == "Chirodzo")
    
    13 để đi từ định dạng dài đến rộng.