尝试在R

时间:2019-02-03 22:20:34

标签: r dplyr

我正在尝试扩展数据帧并设置为矩阵,但是在将“公司”设置为row.name时遇到一些问题。

数据如下:

> head(x)
# A tibble: 6 x 3
  name                markups word        
  <chr>                 <dbl> <chr>       
1 M&T BANK CORP         0.813 interest    
2 L Brands, Inc.        2.26  outcome     
3 AUTOZONE INC          1.08  years       
4 PEPSICO INC           0.699 price       
5 PEPSICO INC           0.699 increase    
6 PROCTER & GAMBLE Co   2.17  restrictions

所以我试图创建一个看起来像这样的矩阵(稀疏):

rowname            markups        word1      word2      word3 ...
M&T BANK CORP       0.813           1         0          0    ….

我的代码如下

library(rsample)
library(Matrix)
words_sparse <- words_final %>% 
  as_tibble() %>%
  count(word, markups) %>%
  spread(word, n, fill = 0) %>%
  column_to_rownames("name") %>%
  as.matrix() %>%
  Matrix(., sparse = TRUE)

似乎没有创建数据,我需要任何帮助。

编辑:

有错误的新数据:

structure(list(name = c("YUM BRANDS INC", "BEST BUY CO INC", 
"UNITEDHEALTH GROUP INC", "ROSS STORES INC", "PEPSICO INC", "XCEL ENERGY INC", 
"HARRIS CORP /DE/", "M&T BANK CORP", "HARRIS CORP /DE/", "XCEL ENERGY INC", 
"ADOBE INC.", "M&T BANK CORP", "BEST BUY CO INC", "UNITED PARCEL SERVICE INC", 
"TYSON FOODS INC", "EBAY INC", "ABBOTT LABORATORIES", "UNITEDHEALTH GROUP INC", 
"EBAY INC", "EBAY INC", "HARRIS CORP /DE/", "IDEXX LABORATORIES INC /DE", 
"BEST BUY CO INC", "Merck & Co., Inc.", "E TRADE FINANCIAL CORP", 
"M&T BANK CORP", "M&T BANK CORP", "PULTEGROUP INC/MI/", "BRISTOL MYERS SQUIBB CO", 
"M&T BANK CORP", "E TRADE FINANCIAL CORP", "L Brands, Inc.", 
"PEPSICO INC", "VARIAN MEDICAL SYSTEMS INC", "Merck & Co., Inc.", 
"M&T BANK CORP", "VARIAN MEDICAL SYSTEMS INC", "XCEL ENERGY INC", 
"IDEXX LABORATORIES INC /DE", "DUKE REALTY CORP", "F5 NETWORKS INC", 
"L Brands, Inc.", "Walmart Inc.", "PEPSICO INC", "TARGET CORP", 
"C H ROBINSON WORLDWIDE INC", "DUKE REALTY CORP", "BEST BUY CO INC", 
"AES CORP", "ADOBE INC.", "BRISTOL MYERS SQUIBB CO", "F5 NETWORKS INC", 
"HARRIS CORP /DE/", "BRISTOL MYERS SQUIBB CO", "UNITED PARCEL SERVICE INC", 
"XCEL ENERGY INC", "TYSON FOODS INC", "Merck & Co., Inc.", "M&T BANK CORP", 
"C H ROBINSON WORLDWIDE INC", "BRISTOL MYERS SQUIBB CO", "BRISTOL MYERS SQUIBB CO", 
"PEPSICO INC", "MICRON TECHNOLOGY INC", "YUM BRANDS INC", "E TRADE FINANCIAL CORP", 
"STARBUCKS CORP", "WESTERN DIGITAL CORP", "Macy's, Inc.", "BRISTOL MYERS SQUIBB CO", 
"PROCTER & GAMBLE Co", "UNITEDHEALTH GROUP INC", "TYSON FOODS INC", 
"Activision Blizzard, Inc.", "ADOBE INC.", "WESTERN DIGITAL CORP", 
"Activision Blizzard, Inc.", "PULTEGROUP INC/MI/", "L Brands, Inc.", 
"Activision Blizzard, Inc.", "TARGET CORP", "E TRADE FINANCIAL CORP", 
"IDEXX LABORATORIES INC /DE", "M&T BANK CORP", "HARRIS CORP /DE/", 
"M&T BANK CORP", "AKAMAI TECHNOLOGIES INC", "DUKE REALTY CORP", 
"ABBOTT LABORATORIES", "TYSON FOODS INC", "PEPSICO INC", "QUALCOMM INC/DE", 
"BEST BUY CO INC", "KELLOGG CO", "AES CORP", "L Brands, Inc.", 
"BALL Corp", "Merck & Co., Inc.", "UNITEDHEALTH GROUP INC", "AES CORP", 
"PROCTER & GAMBLE Co", "EBAY INC", "PULTEGROUP INC/MI/", "HORTON D R INC /DE/", 
"MCCORMICK & CO INC", "KELLOGG CO", "VARIAN MEDICAL SYSTEMS INC", 
"AES CORP", "HARRIS CORP /DE/", "IDEXX LABORATORIES INC /DE", 
"IDEXX LABORATORIES INC /DE", "F5 NETWORKS INC", "PROCTER & GAMBLE Co", 
"HORTON D R INC /DE/", "AKAMAI TECHNOLOGIES INC", "KELLOGG CO", 
"BRISTOL MYERS SQUIBB CO", "EXPEDITORS INTERNATIONAL OF WASHINGTON INC", 
"M&T BANK CORP", "M&T BANK CORP", "HORTON D R INC /DE/", "BRISTOL MYERS SQUIBB CO", 
"M&T BANK CORP", "Merck & Co., Inc.", "TYSON FOODS INC", "BEST BUY CO INC", 
"M&T BANK CORP", "WESTERN DIGITAL CORP", "EBAY INC", "PEPSICO INC", 
"MCCORMICK & CO INC", "TARGET CORP", "BRISTOL MYERS SQUIBB CO", 
"C H ROBINSON WORLDWIDE INC", "CATERPILLAR INC", "ADOBE INC.", 
"BEST BUY CO INC", "M&T BANK CORP", "IDEXX LABORATORIES INC /DE", 
"GENERAL DYNAMICS CORP", "BEST BUY CO INC", "PULTEGROUP INC/MI/", 
"XCEL ENERGY INC", "DUKE REALTY CORP", "AKAMAI TECHNOLOGIES INC", 
"TYSON FOODS INC", "PEPSICO INC", "QUALCOMM INC/DE", "HARRIS CORP /DE/", 
"ADOBE INC.", "DUKE REALTY CORP", "PEPSICO INC", "ABBOTT LABORATORIES", 
"YUM BRANDS INC", "E TRADE FINANCIAL CORP", "ABBOTT LABORATORIES", 
"AKAMAI TECHNOLOGIES INC", "UNITED PARCEL SERVICE INC", "M&T BANK CORP", 
"Merck & Co., Inc.", "HARRIS CORP /DE/", "VARIAN MEDICAL SYSTEMS INC", 
"M&T BANK CORP", "E TRADE FINANCIAL CORP", "M&T BANK CORP", "ADOBE INC.", 
"UNITED PARCEL SERVICE INC", "GENERAL DYNAMICS CORP", "UNITED PARCEL SERVICE INC", 
"XCEL ENERGY INC", "STARBUCKS CORP", "E TRADE FINANCIAL CORP", 
"EXPEDITORS INTERNATIONAL OF WASHINGTON INC", "AKAMAI TECHNOLOGIES INC", 
"ABBOTT LABORATORIES", "QUALCOMM INC/DE", "EBAY INC", "PEPSICO INC", 
"AES CORP", "VARIAN MEDICAL SYSTEMS INC", "F5 NETWORKS INC", 
"EXPEDITORS INTERNATIONAL OF WASHINGTON INC", "HARRIS CORP /DE/", 
"E TRADE FINANCIAL CORP", "HORTON D R INC /DE/", "Merck & Co., Inc.", 
"MCCORMICK & CO INC", "STARBUCKS CORP", "YUM BRANDS INC", "ABBOTT LABORATORIES", 
"ROSS STORES INC", "XCEL ENERGY INC", "Merck & Co., Inc.", "L Brands, Inc.", 
"L Brands, Inc.", "VARIAN MEDICAL SYSTEMS INC", "Macy's, Inc.", 
"XCEL ENERGY INC", "M&T BANK CORP", "AUTOZONE INC", "AKAMAI TECHNOLOGIES INC", 
"M&T BANK CORP", "E TRADE FINANCIAL CORP", "VARIAN MEDICAL SYSTEMS INC", 
"AUTOZONE INC", "M&T BANK CORP", "DUKE REALTY CORP", "HARRIS CORP /DE/", 
"TARGET CORP", "IDEXX LABORATORIES INC /DE", "QUALCOMM INC/DE", 
"M&T BANK CORP", "HARRIS CORP /DE/", "VARIAN MEDICAL SYSTEMS INC", 
"AES CORP", "STARBUCKS CORP", "BALL Corp", "E TRADE FINANCIAL CORP", 
"Merck & Co., Inc.", "DUKE REALTY CORP", "E TRADE FINANCIAL CORP", 
"VARIAN MEDICAL SYSTEMS INC", "Merck & Co., Inc.", "YUM BRANDS INC", 
"Merck & Co., Inc.", "AES CORP", "BRISTOL MYERS SQUIBB CO", "GENERAL DYNAMICS CORP", 
"HARRIS CORP /DE/", "HORTON D R INC /DE/", "GENERAL DYNAMICS CORP", 
"PEPSICO INC", "WESTERN DIGITAL CORP", "UNITEDHEALTH GROUP INC", 
"IDEXX LABORATORIES INC /DE", "KELLOGG CO", "PULTEGROUP INC/MI/", 
"HARRIS CORP /DE/", "UNITED PARCEL SERVICE INC", "F5 NETWORKS INC", 
"E TRADE FINANCIAL CORP", "Merck & Co., Inc.", "AKAMAI TECHNOLOGIES INC", 
"Macy's, Inc.", "WESTERN DIGITAL CORP", "BRISTOL MYERS SQUIBB CO", 
"EBAY INC", "M&T BANK CORP", "IDEXX LABORATORIES INC /DE", "BEST BUY CO INC", 
"E TRADE FINANCIAL CORP", "MACERICH CO", "STARBUCKS CORP", "BRISTOL MYERS SQUIBB CO", 
"Activision Blizzard, Inc.", "M&T BANK CORP", "IDEXX LABORATORIES INC /DE", 
"MACERICH CO", "PEPSICO INC", "TARGET CORP", "UNITEDHEALTH GROUP INC", 
"ABBOTT LABORATORIES", "MICRON TECHNOLOGY INC", "M&T BANK CORP", 
"E TRADE FINANCIAL CORP", "PROCTER & GAMBLE Co", "BRISTOL MYERS SQUIBB CO", 
"ADOBE INC.", "E TRADE FINANCIAL CORP", "WESTERN DIGITAL CORP", 
"HARRIS CORP /DE/", "M&T BANK CORP", "PEPSICO INC", "E TRADE FINANCIAL CORP", 
"AES CORP", "L Brands, Inc.", "TYSON FOODS INC", "L Brands, Inc.", 
"BRISTOL MYERS SQUIBB CO", "PEPSICO INC", "HARRIS CORP /DE/", 
"Merck & Co., Inc.", "STARBUCKS CORP", "XCEL ENERGY INC", "PULTEGROUP INC/MI/", 
"YUM BRANDS INC", "VARIAN MEDICAL SYSTEMS INC", "ADOBE INC.", 
"Walmart Inc.", "VARIAN MEDICAL SYSTEMS INC", "BRISTOL MYERS SQUIBB CO", 
"EBAY INC", "STARBUCKS CORP", "BRISTOL MYERS SQUIBB CO", "DUKE REALTY CORP", 
"C H ROBINSON WORLDWIDE INC", "WESTERN DIGITAL CORP", "Activision Blizzard, Inc.", 
"MACERICH CO", "STARBUCKS CORP", "M&T BANK CORP", "BRISTOL MYERS SQUIBB CO", 
"HORTON D R INC /DE/", "HARRIS CORP /DE/", "BEST BUY CO INC", 
"ADOBE INC.", "HORMEL FOODS CORP /DE/", "EBAY INC", "CATERPILLAR INC", 
"BRISTOL MYERS SQUIBB CO", "VARIAN MEDICAL SYSTEMS INC", "UNITEDHEALTH GROUP INC", 
"ADOBE INC.", "ROSS STORES INC", "WESTERN DIGITAL CORP", "M&T BANK CORP", 
"WESTERN DIGITAL CORP", "Activision Blizzard, Inc.", "Activision Blizzard, Inc.", 
"ABBOTT LABORATORIES", "UNITED PARCEL SERVICE INC", "M&T BANK CORP", 
"IDEXX LABORATORIES INC /DE", "C H ROBINSON WORLDWIDE INC", "STARBUCKS CORP", 
"GENERAL DYNAMICS CORP", "PEPSICO INC", "EXPEDITORS INTERNATIONAL OF WASHINGTON INC", 
"KELLOGG CO", "MACERICH CO", "WESTERN DIGITAL CORP", "UNITEDHEALTH GROUP INC", 
"VARIAN MEDICAL SYSTEMS INC", "WESTERN DIGITAL CORP", "HORTON D R INC /DE/", 
"PROCTER & GAMBLE Co", "AKAMAI TECHNOLOGIES INC", "DUKE REALTY CORP", 
"TYSON FOODS INC", "PEPSICO INC", "F5 NETWORKS INC", "BRISTOL MYERS SQUIBB CO", 
"MICRON TECHNOLOGY INC", "HARRIS CORP /DE/", "MACERICH CO", "MACERICH CO", 
"AES CORP", "MICRON TECHNOLOGY INC", "L Brands, Inc.", "WESTERN DIGITAL CORP", 
"VARIAN MEDICAL SYSTEMS INC", "L Brands, Inc.", "BRISTOL MYERS SQUIBB CO", 
"WESTERN DIGITAL CORP", "ADOBE INC.", "ROSS STORES INC", "VARIAN MEDICAL SYSTEMS INC", 
"PEPSICO INC", "YUM BRANDS INC", "UNITED PARCEL SERVICE INC", 
"Activision Blizzard, Inc.", "VARIAN MEDICAL SYSTEMS INC", "MACERICH CO", 
"GENERAL DYNAMICS CORP", "ADOBE INC.", "HARRIS CORP /DE/", "TARGET CORP", 
"E TRADE FINANCIAL CORP", "VARIAN MEDICAL SYSTEMS INC", "L Brands, Inc.", 
"HARRIS CORP /DE/", "Macy's, Inc.", "UNITEDHEALTH GROUP INC", 
"ADOBE INC.", "TYSON FOODS INC", "BEST BUY CO INC", "UNITED PARCEL SERVICE INC", 
"ROSS STORES INC", "VARIAN MEDICAL SYSTEMS INC", "M&T BANK CORP", 
"ADOBE INC.", "Activision Blizzard, Inc.", "VARIAN MEDICAL SYSTEMS INC", 
"BRISTOL MYERS SQUIBB CO", "PEPSICO INC", "BALL Corp", "DUKE REALTY CORP", 
"AUTOZONE INC", "PULTEGROUP INC/MI/", "HARRIS CORP /DE/", "ROSS STORES INC", 
"XCEL ENERGY INC", "PEPSICO INC", "BRISTOL MYERS SQUIBB CO", 
"XCEL ENERGY INC", "Macy's, Inc.", "E TRADE FINANCIAL CORP", 
"AES CORP", "M&T BANK CORP", "M&T BANK CORP", "E TRADE FINANCIAL CORP", 
"YUM BRANDS INC", "L Brands, Inc.", "STARBUCKS CORP", "UNITEDHEALTH GROUP INC", 
"ADOBE INC.", "PULTEGROUP INC/MI/", "GENERAL DYNAMICS CORP", 
"HORTON D R INC /DE/", "YUM BRANDS INC", "STARBUCKS CORP", "PEPSICO INC", 
"PEPSICO INC", "BEST BUY CO INC", "Activision Blizzard, Inc.", 
"EBAY INC", "UNITED PARCEL SERVICE INC", "VARIAN MEDICAL SYSTEMS INC", 
"ADOBE INC.", "HORTON D R INC /DE/", "HARRIS CORP /DE/", "TYSON FOODS INC", 
"L Brands, Inc.", "EXPEDITORS INTERNATIONAL OF WASHINGTON INC", 
"BRISTOL MYERS SQUIBB CO", "Merck & Co., Inc.", "PROCTER & GAMBLE Co", 
"M&T BANK CORP", "Merck & Co., Inc.", "Activision Blizzard, Inc.", 
"M&T BANK CORP", "MICRON TECHNOLOGY INC", "Merck & Co., Inc.", 
"KELLOGG CO", "AES CORP", "E TRADE FINANCIAL CORP", "Merck & Co., Inc.", 
"M&T BANK CORP", "Merck & Co., Inc.", "AES CORP", "Merck & Co., Inc.", 
"ABBOTT LABORATORIES", "VARIAN MEDICAL SYSTEMS INC", "VARIAN MEDICAL SYSTEMS INC", 
"Merck & Co., Inc.", "PEPSICO INC", "Merck & Co., Inc.", "MACERICH CO", 
"UNITEDHEALTH GROUP INC", "IDEXX LABORATORIES INC /DE", "Merck & Co., Inc.", 
"E TRADE FINANCIAL CORP", "PEPSICO INC", "IDEXX LABORATORIES INC /DE", 
"ABBOTT LABORATORIES", "GENERAL DYNAMICS CORP", "BRISTOL MYERS SQUIBB CO", 
"Activision Blizzard, Inc.", "HARRIS CORP /DE/", "DUKE REALTY CORP", 
"PROCTER & GAMBLE Co", "MICRON TECHNOLOGY INC", "CATERPILLAR INC", 
"MACERICH CO", "L Brands, Inc.", "BRISTOL MYERS SQUIBB CO", "M&T BANK CORP", 
"PEPSICO INC", "VARIAN MEDICAL SYSTEMS INC", "STARBUCKS CORP", 
"KELLOGG CO", "EXPEDITORS INTERNATIONAL OF WASHINGTON INC", "YUM BRANDS INC", 
"EBAY INC", "Activision Blizzard, Inc.", "HARRIS CORP /DE/", 
"IDEXX LABORATORIES INC /DE", "Macy's, Inc.", "Macy's, Inc.", 
"M&T BANK CORP", "VARIAN MEDICAL SYSTEMS INC", "ADOBE INC.", 
"EBAY INC", "PEPSICO INC", "Merck & Co., Inc.", "PEPSICO INC", 
"UNITEDHEALTH GROUP INC", "BALL Corp", "XCEL ENERGY INC", "AES CORP", 
"UNITED PARCEL SERVICE INC", "MICRON TECHNOLOGY INC", "VARIAN MEDICAL SYSTEMS INC", 
"MACERICH CO", "YUM BRANDS INC", "PROGRESSIVE CORP/OH/", "ADOBE INC.", 
"WESTERN DIGITAL CORP", "HORTON D R INC /DE/", "EXPEDITORS INTERNATIONAL OF WASHINGTON INC"
), markups = c(3.54749202728271, 0.583845237269998, 2.75451855920255, 
2.38408696651459, 1.54455975536257, 0.969432728365064, 0.248355126939714, 
2.19291824661195, 0.248355126939714, 0.969432728365064, 1.79961029067636, 
2.19291824661195, 0.583845237269998, 1.56372799910605, 3.61782895494252, 
2.01324200443923, 3.74784562177956, 2.75451855920255, 2.01324200443923, 
2.01324200443923, 0.248355126939714, 3.70747435744852, 0.583845237269998, 
2.99019916635007, 0.555445314384997, 2.19291824661195, 2.19291824661195, 
3.94206533674151, 0.606846420094371, 2.19291824661195, 0.555445314384997, 
2.63140281662345, 1.54455975536257, 3.22012562211603, 2.99019916635007, 
2.19291824661195, 3.22012562211603, 0.969432728365064, 3.70747435744852, 
3.60303373076022, 3.2524936767295, 2.63140281662345, 1.60698625259101, 
1.54455975536257, 3.80723156034946, 0.0777335539460182, 3.60303373076022, 
0.583845237269998, 2.73847420793027, 1.79961029067636, 0.606846420094371, 
3.2524936767295, 0.248355126939714, 0.606846420094371, 1.56372799910605, 
0.969432728365064, 3.61782895494252, 2.99019916635007, 2.19291824661195, 
0.0777335539460182, 0.606846420094371, 0.606846420094371, 1.54455975536257, 
0.734132847748697, 3.54749202728271, 0.555445314384997, 0.810196593403816, 
1.89508401695639, 2.3023427547887, 0.606846420094371, 0.691112162545323, 
2.75451855920255, 3.61782895494252, 2.05052343010902, 1.79961029067636, 
1.89508401695639, 2.05052343010902, 3.94206533674151, 2.63140281662345, 
2.05052343010902, 3.80723156034946, 0.555445314384997, 3.70747435744852, 
2.19291824661195, 0.248355126939714, 2.19291824661195, 0.577226044610143, 
3.60303373076022, 3.74784562177956, 3.61782895494252, 1.54455975536257, 
1.7969083795324, 0.583845237269998, 2.09497321862727, 2.73847420793027, 
2.63140281662345, 2.18435965012759, 2.99019916635007, 2.75451855920255, 
2.73847420793027, 0.691112162545323, 2.01324200443923, 3.94206533674151, 
0.314842415973544, 2.24657814670354, 2.09497321862727, 3.22012562211603, 
2.73847420793027, 0.248355126939714, 3.70747435744852, 3.70747435744852, 
3.2524936767295, 0.691112162545323, 0.314842415973544, 0.577226044610143, 
2.09497321862727, 0.606846420094371, 3.53308956604451, 2.19291824661195, 
2.19291824661195, 0.314842415973544, 0.606846420094371, 2.19291824661195, 
2.99019916635007, 3.61782895494252, 0.583845237269998, 2.19291824661195, 
1.89508401695639, 2.01324200443923, 1.54455975536257, 2.24657814670354, 
3.80723156034946, 0.606846420094371, 0.0777335539460182, 0.756775739602745, 
1.79961029067636, 0.583845237269998, 2.19291824661195, 3.70747435744852, 
1.64627963770181, 0.583845237269998, 3.94206533674151, 0.969432728365064, 
3.60303373076022, 0.577226044610143, 3.61782895494252, 1.54455975536257, 
1.7969083795324, 0.248355126939714, 1.79961029067636, 3.60303373076022, 
1.54455975536257, 3.74784562177956, 3.54749202728271, 0.555445314384997, 
3.74784562177956, 0.577226044610143, 1.56372799910605, 2.19291824661195, 
2.99019916635007, 0.248355126939714, 3.22012562211603, 2.19291824661195, 
0.555445314384997, 2.19291824661195, 1.79961029067636, 1.56372799910605, 
1.64627963770181, 1.56372799910605, 0.969432728365064, 0.810196593403816, 
0.555445314384997, 3.53308956604451, 0.577226044610143, 3.74784562177956, 
1.7969083795324, 2.01324200443923, 1.54455975536257, 2.73847420793027, 
3.22012562211603, 3.2524936767295, 3.53308956604451, 0.248355126939714, 
0.555445314384997, 0.314842415973544, 2.99019916635007, 2.24657814670354, 
0.810196593403816, 3.54749202728271, 3.74784562177956, 2.38408696651459, 
0.969432728365064, 2.99019916635007, 2.63140281662345, 2.63140281662345, 
3.22012562211603, 2.3023427547887, 0.969432728365064, 2.19291824661195, 
3.51697555743158, 0.577226044610143, 2.19291824661195, 0.555445314384997, 
3.22012562211603, 3.51697555743158, 2.19291824661195, 3.60303373076022, 
0.248355126939714, 3.80723156034946, 3.70747435744852, 1.7969083795324, 
2.19291824661195, 0.248355126939714, 3.22012562211603, 2.73847420793027, 
0.810196593403816, 2.18435965012759, 0.555445314384997, 2.99019916635007, 
3.60303373076022, 0.555445314384997, 3.22012562211603, 2.99019916635007, 
3.54749202728271, 2.99019916635007, 2.73847420793027, 0.606846420094371, 
1.64627963770181, 0.248355126939714, 0.314842415973544, 1.64627963770181, 
1.54455975536257, 1.89508401695639, 2.75451855920255, 3.70747435744852, 
2.09497321862727, 3.94206533674151, 0.248355126939714, 1.56372799910605, 
3.2524936767295, 0.555445314384997, 2.99019916635007, 0.577226044610143, 
2.3023427547887, 1.89508401695639, 0.606846420094371, 2.01324200443923, 
2.19291824661195, 3.70747435744852, 0.583845237269998, 0.555445314384997, 
1.44202467985451, 0.810196593403816, 0.606846420094371, 2.05052343010902, 
2.19291824661195, 3.70747435744852, 1.44202467985451, 1.54455975536257, 
3.80723156034946, 2.75451855920255, 3.74784562177956, 0.734132847748697, 
2.19291824661195, 0.555445314384997, 0.691112162545323, 0.606846420094371, 
1.79961029067636, 0.555445314384997, 1.89508401695639, 0.248355126939714, 
2.19291824661195, 1.54455975536257, 0.555445314384997, 2.73847420793027, 
2.63140281662345, 3.61782895494252, 2.63140281662345, 0.606846420094371, 
1.54455975536257, 0.248355126939714, 2.99019916635007, 0.810196593403816, 
0.969432728365064, 3.94206533674151, 3.54749202728271, 3.22012562211603, 
1.79961029067636, 1.60698625259101, 3.22012562211603, 0.606846420094371, 
2.01324200443923, 0.810196593403816, 0.606846420094371, 3.60303373076022, 
0.0777335539460182, 1.89508401695639, 2.05052343010902, 1.44202467985451, 
0.810196593403816, 2.19291824661195, 0.606846420094371, 0.314842415973544, 
0.248355126939714, 0.583845237269998, 1.79961029067636, 2.91237778589129, 
2.01324200443923, 0.756775739602745, 0.606846420094371, 3.22012562211603, 
2.75451855920255, 1.79961029067636, 2.38408696651459, 1.89508401695639, 
2.19291824661195, 1.89508401695639, 2.05052343010902, 2.05052343010902, 
3.74784562177956, 1.56372799910605, 2.19291824661195, 3.70747435744852, 
0.0777335539460182, 0.810196593403816, 1.64627963770181, 1.54455975536257, 
3.53308956604451, 2.09497321862727, 1.44202467985451, 1.89508401695639, 
2.75451855920255, 3.22012562211603, 1.89508401695639, 0.314842415973544, 
0.691112162545323, 0.577226044610143, 3.60303373076022, 3.61782895494252, 
1.54455975536257, 3.2524936767295, 0.606846420094371, 0.734132847748697, 
0.248355126939714, 1.44202467985451, 1.44202467985451, 2.73847420793027, 
0.734132847748697, 2.63140281662345, 1.89508401695639, 3.22012562211603, 
2.63140281662345, 0.606846420094371, 1.89508401695639, 1.79961029067636, 
2.38408696651459, 3.22012562211603, 1.54455975536257, 3.54749202728271, 
1.56372799910605, 2.05052343010902, 3.22012562211603, 1.44202467985451, 
1.64627963770181, 1.79961029067636, 0.248355126939714, 3.80723156034946, 
0.555445314384997, 3.22012562211603, 2.63140281662345, 0.248355126939714, 
2.3023427547887, 2.75451855920255, 1.79961029067636, 3.61782895494252, 
0.583845237269998, 1.56372799910605, 2.38408696651459, 3.22012562211603, 
2.19291824661195, 1.79961029067636, 2.05052343010902, 3.22012562211603, 
0.606846420094371, 1.54455975536257, 2.18435965012759, 3.60303373076022, 
3.51697555743158, 3.94206533674151, 0.248355126939714, 2.38408696651459, 
0.969432728365064, 1.54455975536257, 0.606846420094371, 0.969432728365064, 
2.3023427547887, 0.555445314384997, 2.73847420793027, 2.19291824661195, 
2.19291824661195, 0.555445314384997, 3.54749202728271, 2.63140281662345, 
0.810196593403816, 2.75451855920255, 1.79961029067636, 3.94206533674151, 
1.64627963770181, 0.314842415973544, 3.54749202728271, 0.810196593403816, 
1.54455975536257, 1.54455975536257, 0.583845237269998, 2.05052343010902, 
2.01324200443923, 1.56372799910605, 3.22012562211603, 1.79961029067636, 
0.314842415973544, 0.248355126939714, 3.61782895494252, 2.63140281662345, 
3.53308956604451, 0.606846420094371, 2.99019916635007, 0.691112162545323, 
2.19291824661195, 2.99019916635007, 2.05052343010902, 2.19291824661195, 
0.734132847748697, 2.99019916635007, 2.09497321862727, 2.73847420793027, 
0.555445314384997, 2.99019916635007, 2.19291824661195, 2.99019916635007, 
2.73847420793027, 2.99019916635007, 3.74784562177956, 3.22012562211603, 
3.22012562211603, 2.99019916635007, 1.54455975536257, 2.99019916635007, 
1.44202467985451, 2.75451855920255, 3.70747435744852, 2.99019916635007, 
0.555445314384997, 1.54455975536257, 3.70747435744852, 3.74784562177956, 
1.64627963770181, 0.606846420094371, 2.05052343010902, 0.248355126939714, 
3.60303373076022, 0.691112162545323, 0.734132847748697, 0.756775739602745, 
1.44202467985451, 2.63140281662345, 0.606846420094371, 2.19291824661195, 
1.54455975536257, 3.22012562211603, 0.810196593403816, 2.09497321862727, 
3.53308956604451, 3.54749202728271, 2.01324200443923, 2.05052343010902, 
0.248355126939714, 3.70747435744852, 2.3023427547887, 2.3023427547887, 
2.19291824661195, 3.22012562211603, 1.79961029067636, 2.01324200443923, 
1.54455975536257, 2.99019916635007, 1.54455975536257, 2.75451855920255, 
2.18435965012759, 0.969432728365064, 2.73847420793027, 1.56372799910605, 
0.734132847748697, 3.22012562211603, 1.44202467985451, 3.54749202728271, 
2.34714597277343, 1.79961029067636, 1.89508401695639, 0.314842415973544, 
3.53308956604451), word = c("obligations", "excluding", "currency", 
"less", "charges", "estimated", "improvements", "income", "fiscal", 
"years", "forfeitures", "accounts", "facility", "utilize", "liabilities", 
"net", "plans", "income", "account", "income", "due", "changes", 
"segment", "postretirement", "year", "intends", "unfavorable", 
"land", "balance", "yield", "clearance", "operating", "policies", 
"software", "investment", "reduced", "primarily", "levels", "sales", 
"securities", "september", "name", "costs", "growth", "lower", 
"percent", "evaluate", "restructuring", "dollars", "significant", 
"mail", "respective", "fiscal", "efforts", "recoverable", "recover", 
"income", "sectors", "backed", "number", "growth", "become", 
"activity", "estimate", "due", "agency", "decreased", "realized", 
"sell", "higher", "acquisition", "international", "increase", 
"related", "recognized", "net", "call", "interest", "executive", 
"table", "excluded", "periods", "group", "refer", "million", 
"annual", "measurement", "value", "uses", "information", "america", 
"required", "within", "beyond", "utilities", "currency", "including", 
"connection", "mix", "lost", "plans", "competitors", "decisions", 
"markets", "growth", "funding", "addition", "item", "contracts", 
"resulting", "due", "must", "volume", "negatively", "year", "due", 
"generic", "locations", "determining", "increase", "credit", 
"estimate", "million", "impact", "obligations", "cash", "contribution", 
"conditions", "table", "capital", "debt", "call", "ratings", 
"exercise", "purpose", "year", "store", "expenses", "total", 
"readily", "debt", "longer", "unfavorable", "properties", "interest", 
"used", "digit", "unit", "revenue", "decline", "compensation", 
"businesses", "signed", "fourth", "decrease", "revised", "principal", 
"american", "securities", "annual", "time", "businesses", "real", 
"balance", "consider", "partnership", "freight", "systems", "profit", 
"gas", "equity", "loans", "net", "policy", "includes", "results", 
"criteria", "fair", "salvador", "table", "income", "liquidity", 
"gain", "outflow", "level", "approximately", "low", "operating", 
"business", "approximately", "climate", "share", "available", 
"assets", "periods", "applied", "historical", "smart", "charge", 
"fiscal", "revenue", "majority", "management", "increase", "increase", 
"bank", "million", "communications", "operations", "dollars", 
"paid", "increase", "certain", "extended", "impairment", "cost", 
"offset", "brokerage", "financial", "operations", "bank", "total", 
"considers", "note", "owed", "increased", "stock", "letters", 
"discontinued", "increased", "paid", "based", "manufacturers", 
"completion", "diagnostics", "share", "available", "date", "determine", 
"years", "actual", "control", "program", "discount", "products", 
"one", "laws", "core", "less", "new", "loan", "joint", "states", 
"rate", "recognized", "portion", "future", "weighted", "item", 
"pronouncements", "goodwill", "launched", "differ", "respectively", 
"apple", "validity", "made", "expenses", "remaining", "remain", 
"income", "yield", "independent", "addition", "lease", "items", 
"segregated", "results", "valuation", "raw", "million", "term", 
"revenues", "dollars", "models", "fair", "fiscal", "revenues", 
"investment", "used", "based", "service", "equity", "businesses", 
"party", "plans", "exchange", "amounts", "variations", "combination", 
"investment", "affect", "rate", "fiscal", "however", "due", "total", 
"metrics", "half", "pharmaceutical", "total", "liabilities", 
"remaining", "revolving", "sets", "table", "continue", "statement", 
"subscriptions", "agreement", "settled", "represents", "actual", 
"possible", "consolidated", "activity", "interest", "addition", 
"deferred", "amended", "center", "expected", "already", "accrue", 
"million", "digit", "liability", "affect", "mix", "see", "stock", 
"securities", "prices", "lower", "detailed", "many", "expect", 
"million", "improvement", "ship", "systems", "term", "certain", 
"unit", "creative", "lease", "value", "favorable", "store", "gallon", 
"product", "amounts", "payments", "cash", "plan", "goodwill", 
"based", "development", "manufactures", "value", "give", "company", 
"medical", "using", "percent", "interest", "overall", "annual", 
"improved", "million", "sales", "north", "necessary", "inception", 
"financial", "reversal", "held", "cost", "increased", "fiscal", 
"statements", "operations", "evaluated", "sales", "sale", "impairment", 
"party", "receipt", "long", "partially", "decline", "made", "used", 
"policy", "used", "economy", "community", "worth", "active", 
"approximately", "shown", "share", "evaluates", "discontinued", 
"table", "reflected", "domestic", "provided", "internal", "benefit", 
"segments", "due", "borrowing", "rate", "contents", "decisions", 
"without", "fails", "examining", "million", "buyers", "increase", 
"portion", "unfavorable", "way", "file", "owned", "business", 
"patent", "related", "developed", "acquisition", "fiscal", "changes", 
"annual", "return", "brought", "interest", "relatively", "activities", 
"new", "period", "consolidated", "assumptions", "value", "general", 
"market", "certain", "well", "million", "registration", "addition", 
"changes", "method", "prospects", "taxes", "advisory", "however", 
"growing", "year", "expense", "securities", "resulting", "rate", 
"aggregate", "previously", "revenue", "likely", "credit", "real", 
"depends", "subjective", "activities", "beyond", "states", "recorded", 
"among", "consumer", "contents", "counsel", "time", "research", 
"year", "recovery", "new", "board", "classified", "meet", "continue", 
"statements")), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-500L))

1 个答案:

答案 0 :(得分:3)

问题是您在name之后丢失了count列。假设每个公司都有不同的标记,我们可以使用

words_sparse <- words_final %>%
  as_tibble() %>%
  count(word, markups, CompanyNames = name) %>%
  spread(word, n, fill = 0) %>%
  column_to_rownames("CompanyNames") %>%
  as.matrix() %>%
  Matrix(., sparse = TRUE)

计数时,我将name列重命名为CompanyNames,因为“名称”本身是words列的元素之一。