pivot和count两个变量的成员资格

时间:2017-02-19 11:37:18

标签: r dataframe

我在R中有一个数据框,如下所示:

p+geom_vline(xintercept=as.numeric(as.POSIXct("2016-01-01", format="%Y-%m-%d")))

此处<LinearLayout android:layout_width="match_parent" android:layout_height="1dp" android:layout_below="@id/widget_title_id" android:background="@android:color/darker_gray"/> int main() { setlocale(LC_ALL, "rus"); int n; cin >> n; base *a = new base[n]; char symb; char sym[20]; int j = 0; for (int i = 0; i<n; i++) { do { symb = getchar(); sym[j] = symb; j++; } while (symb != ' '); for (int k = 0; k<j; k++) //check cout << sym[k] << ' '; //check a[i].name = new char[j]; strncpy(sym, a[i].name, j); j = 0; } } 是1到5之间的分类变量。

我想创建一个矩阵,其中列标题为 def authenticated_header(user) token = Knock::AuthToken.new(payload: { sub: user.id }).token { 'Authorization': "Bearer #{token}" } end describe 'GET /users?me=true' do URL = '/v1/users?me=true' AUTH_URL = '/user_token' context 'when the request with NO authentication header' do it 'should return unauth for retrieve current user info before login' do get URL expect(response).to have_http_status(:unauthorized) end end context 'when the request contains an authentication header' do it 'should return the user info' do user = create(:user) get URL, headers: authenticated_header(user) puts response.body end end end 1到5,行标题为df1 <- data.frame(id = letters[seq( from = 1, to = 20 )], var1 = sample(1:5,20,replace=T), var2 = sample(1:5,20,replace=T)) 1到5,矩阵由填入的条目数填充属于每个小组。

我已尝试使用var1,查看var2包,发现类似this的问题,但我无法适应法案。

2 个答案:

答案 0 :(得分:1)

使用reshape2的dcast

df1 %>% reshape2::dcast(var2~var1)
reshape2::dcast(df1,var2 ~ var1)

答案 1 :(得分:1)

library(dplyr)
library(tidyr)

set.seed(1)

df1 <- data.frame(id = letters[seq( from = 1, to = 20 )],
                  var1 = sample(1:5,20,replace=T),
                  var2 = sample(1:5,20,replace=T))

df1

#    id var1 var2
# 1   a    2    5
# 2   b    2    2
# 3   c    3    4
# 4   d    5    1
# 5   e    2    2
# 6   f    5    2
# 7   g    5    1
# 8   h    4    2
# 9   i    4    5
# 10  j    1    2
# 11  k    2    3
# 12  l    1    3
# 13  m    4    3
# 14  n    2    1
# 15  o    4    5
# 16  p    3    4
# 17  q    4    4
# 18  r    5    1
# 19  s    2    4
# 20  t    4    3


df1 %>%
  count(var1,var2) %>%                     # count how many times you have each combination
  ungroup %>%
  mutate(var1 = paste0("var1_",var1)) %>%  # update variable values
  spread(var1,n, fill=0) %>%               # reshape dataset
  mutate(var2 = paste0("var2_",var2)) %>%  # update variable values
  print() -> df2

# # A tibble: 5 × 6
#     var2 var1_1 var1_2 var1_3 var1_4 var1_5
#    <chr>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>
# 1 var2_1      0      1      0      0      3
# 2 var2_2      1      2      0      1      1
# 3 var2_3      1      1      0      2      0
# 4 var2_4      0      1      2      1      0
# 5 var2_5      0      1      0      2      0

如果您真的希望将var2值作为行名而不是​​列添加此

df2 = data.frame(df2)
row.names(df2) = df2$var2
df2$var2 = NULL

df2

#        var1_1 var1_2 var1_3 var1_4 var1_5
# var2_1      0      1      0      0      3
# var2_2      1      2      0      1      1
# var2_3      1      1      0      2      0
# var2_4      0      1      2      1      0
# var2_5      0      1      0      2      0