将列中的唯一值更改为两个特定值之一

时间:2019-09-24 00:10:30

标签: r sorting dplyr

摘要

在更改数据框的格式之前,我正在尝试组织一个数据框。在我的data.frame中,值归为2个因子(factor1factor2)之下。 factor2中的级别成对分组(例如13和17)在factor 1下。在每对factor2中,我想将一个数字重命名为A,将另一个重命名为B。重命名顺序是任意的,值可以为AB。从前面的示例得出,只要给定数字A而给定另一个B,则13或17可以是A或B。请注意,factor1中的某些级别没有一对数据,并且可能只有一对1。

此后,我想将数据框重塑为另一种格式,如下所示(请参阅#3)

这是一个示例数据。

sampledata <- data.frame(factor0 = c("A", "A", "H", "H", "A", "A", "H", "H", "A", "A",
                                     "H", "H", "A", "A", "H", "H", "A", "A", "H", "H"), 
                         factor1 = c("1", "1", "2", "2", "3", "3", "4", "4", "5", "5", 
                                     "6", "6", "7", "7", "8", "8", "9", "9", "10", "10"),
                         factor2 = c("13", "17", "14", "18", "5", "15", "19", "16", "20", "17",
                                     "18", "19", "21", "24", "27", "28", "30", "30", "17", "18"), 
                         value1 = c(1, 5, 6, 2, 6, 8, 10, 21, 30, 5, 3, 5, 4, 50, 4, 7, 15, 48, 20, 21,
                         value1.se = c(0.5, 0.5, 0.6, 0.2, 0.6, 0.8, 0.10, 0.21, 0.30, 0.5, 
                                                  0.3, 0.5, 0.4, 1.50, 1.4, 1.7, 1.5, 0.48, 2.1, 21)))

1。首先,数据表将如下所示:

> sampledata
#      factor0   factor1   factor2   value1   value1.se
# 1    A         1         13        1        0.5
# 2    A         1         17        5        0.5
# 3    H         1         14        6        0.6
# 4    H         1         18        2        0.2
# 5    A         2         5         6        0.6
# 6    A         2         15        8        0.8
# 7    H         2         19        10       0.10
# 8    H         2         16        21       0.21

2。之后,该表将如下所示:

> sampledata
#      factor0    factor1   factor2   value1   value1.se
# 1    A          1         A         1        0.5
# 2    A          1         B         5        0.5
# 3    H          1         A         6        0.6
# 4    H          1         B         2        0.2
# 5    A          2         A         6        0.6
# 6    A          2         B         8        0.8
# 7    H          2         A        10       0.10
# 8    H          2         B        21       0.21

3。最后,数据表应如下所示:

> sampledata
#      factor0   factor1   value_A   value_B   se_A   se_B
# 1    A         1         1         5         0.5    0.5
# 2    H         1         6         2         0.6    0.2
# 3    A         2         6         8         0.6    0.8
# 4    H         2         10        21        0.10   0.21

我还有另一个要归类于(factor0)的因素,但是我认为我可以自己进行管理。我要以这种方式组织数据的主要原因是,我可以将value_AvalueB涂上颜色,从而使factor0 import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.AppCompatAutoCompleteTextView; import android.content.Context; import android.os.Build; import android.os.Bundle; import android.text.InputType; import android.view.View; import android.view.inputmethod.InputMethodManager; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; import android.widget.Button; import android.widget.TextView; import com.google.android.material.internal.Experimental; public class MainActivity extends AppCompatActivity { private static final String[] TEAMS_A = new String[]{ "Arizona Diamondbacks", "Atlanta Braves", "Baltimore Orioles", "Boston Red Sox", "Chicago Cubs", "Chicago White Sox", "Cincinnati Reds", "Cleveland Indians", "Colorado Rockies", "Detroit Tigers", "Miami Marins", "Houston Astros", "Kansas City Royals", "Los Angeles Angels", "Los Angeles Dodgers", "Milwaukee Brewers", "Minnesota Twins", "New York Mets", "New York Yankess", "Oakland Athletics", "Philadelphia Phillies", "Pittsburgh Pirates", "St. Louis Cardinals", "San Diego Padres", "San Francisco Giants", "Seattle Mariners", "Tampa Bay Rays", "Texas Rangers", "Toronto Blue Jays", "Washington Nationals" }; private static final String[] TEAMS_B = new String[]{ "Arizona Diamondbacks", "Atlanta Braves", "Baltimore Orioles", "Boston Red Sox", "Chicago Cubs", "Chicago White Sox", "Cincinnati Reds", "Cleveland Indians", "Colorado Rockies", "Detroit Tigers", "Miami Marins", "Houston Astros", "Kansas City Royals", "Los Angeles Angels", "Los Angeles Dodgers", "Milwaukee Brewers", "Minnesota Twins", "New York Mets", "New York Yankess", "Oakland Athletics", "Philadelphia Phillies", "Pittsburgh Pirates", "St. Louis Cardinals", "San Diego Padres", "San Francisco Giants", "Seattle Mariners", "Tampa Bay Rays", "Texas Rangers", "Toronto Blue Jays", "Washington Nationals" }; int scoreTeamA = 0; int scoreTeamB = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ArrayAdapter<String> adapter = new ArrayAdapter<>(this, R.layout.dropdown_menu_popup_item, TEAMS_A); final AutoCompleteTextView filledExposedDropdown = findViewById(R.id.filled_exposed_dropdown_team_a); filledExposedDropdown.setAdapter(adapter); ArrayAdapter<String> adapter2 = new ArrayAdapter<>(this, R.layout.dropdown_menu_popup_item, TEAMS_B); final AutoCompleteTextView filledExposedDropdown2 = findViewById(R.id.filled_exposed_dropdown_team_b); filledExposedDropdown2.setAdapter(adapter2); displayForTeamA(0); displayForTeamB(0); } /** * Increase the score for Team A by 4 points. */ public void addFourForTeamA(View v) { scoreTeamA = scoreTeamA + 4; displayForTeamA(scoreTeamA); } /** * Increase the score for Team A by 3 points. */ public void addThreeForTeamA(View v) { scoreTeamA = scoreTeamA + 3; displayForTeamA(scoreTeamA); } /** * Increase the score for Team A by 2 points. */ public void addTwoForTeamA(View v) { scoreTeamA = scoreTeamA + 2; displayForTeamA(scoreTeamA); } /** * Increase the score for Team A by 1 point. */ public void addOneForTeamA(View v) { scoreTeamA = scoreTeamA + 1; displayForTeamA(scoreTeamA); } /** * Increase the score for Team B by 4 points. */ public void addFourForTeamB(View v) { scoreTeamB = scoreTeamB + 4; displayForTeamB(scoreTeamB); } /** * Increase the score for Team B by 3 points. */ public void addThreeForTeamB(View v) { scoreTeamB = scoreTeamB + 3; displayForTeamB(scoreTeamB); } /** * Increase the score for Team B by 2 points. */ public void addTwoForTeamB(View v) { scoreTeamB = scoreTeamB + 2; displayForTeamB(scoreTeamB); } /** * Increase the score for Team B by 1 point. */ public void addOneForTeamB(View v) { scoreTeamB = scoreTeamB + 1; displayForTeamB(scoreTeamB); } /** * Resets the score for both teams back to 0. */ public void resetScore(View v) { scoreTeamA = 0; scoreTeamB = 0; displayForTeamA(scoreTeamA); displayForTeamB(scoreTeamB); clearListSelection(); } /** * Displays the given score for Team A. */ public void displayForTeamA(int score) { TextView scoreView = (TextView) findViewById(R.id.team_a_score); scoreView.setText(String.valueOf(score)); } /** * Displays the given score for Team B. */ public void displayForTeamB(int score) { TextView scoreView = (TextView) findViewById(R.id.team_b_score); scoreView.setText(String.valueOf(score)); } public void clearListSelection() { AutoCompleteTextView filledExposedDropdown = findViewById(R.id.filled_exposed_dropdown_team_a); filledExposedDropdown.clearListSelection(); } } 相互绘制。

2 个答案:

答案 0 :(得分:3)

您可以使用分组数据的长度将factor2更改为值AB,然后使用pivot_wider()对其进行整形(需要tidyr v1.0.0 )。

library(dplyr)
library(tidyr)

sampledata %>%
  group_by(factor0, factor1) %>%
  mutate(factor2 = c("A", "B")[sequence(n())]) %>%
  pivot_wider(names_from = factor2, values_from = c(value1, value1.se))

# A tibble: 10 x 6
# Groups:   factor0, factor1 [20]
   factor0 factor1 value1_A value1_B value1.se_A value1.se_B
   <fct>   <fct>      <dbl>    <dbl>       <dbl>       <dbl>
 1 A       1              1        5         0.5        0.5 
 2 H       2              6        2         0.6        0.2 
 3 A       3              6        8         0.6        0.8 
 4 H       4             10       21         0.1        0.21
 5 A       5             30        5         0.3        0.5 
 6 H       6              3        5         0.3        0.5 
 7 A       7              4       50         0.4        1.5 
 8 H       8              4        7         1.4        1.7 
 9 A       9             15       48         1.5        0.48
10 H       10            20       21         2.1       21  

答案 1 :(得分:0)

使用data.table,我们可以首先更改每个组中的factor2值,然后使用dcast将数据从长格式转换为宽格式。

library(data.table)

setDT(sampledata)[, factor2 := factor(LETTERS[1:2]), .(factor0, factor1)]
dcast(sampledata, factor0 + factor1 ~ factor2, value.var = c("value1", "value1.se"))

#    factor0 factor1 value1_A value1_B value1.se_A value1.se_B
# 1:       A       1        1        5         0.5        0.50
# 2:       A       3        6        8         0.6        0.80
# 3:       A       5       30        5         0.3        0.50
# 4:       A       7        4       50         0.4        1.50
# 5:       A       9       15       48         1.5        0.48
# 6:       H      10       20       21         2.1       21.00
# 7:       H       2        6        2         0.6        0.20
# 8:       H       4       10       21         0.1        0.21
# 9:       H       6        3        5         0.3        0.50
#10:       H       8        4        7         1.4        1.70