Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 数据帧在使用sub()时丢失格式_R_Dataframe - Fatal编程技术网

R 数据帧在使用sub()时丢失格式

R 数据帧在使用sub()时丢失格式,r,dataframe,R,Dataframe,我有一个数据帧,看起来有点像这样: ID Hero Win Rate Matches Played KDA Ratio Wraith King Abaddon Lich Lycan Centaur Warrunner Zeus Necrophos Death Prophet 27 Slardar 52.32% 2,741,067 2.42 -0.63% -2.16% -3.01% 2.40%

我有一个数据帧,看起来有点像这样:

   ID          Hero Win Rate Matches Played KDA Ratio Wraith King Abaddon   Lich  Lycan Centaur Warrunner   Zeus Necrophos Death Prophet
27          Slardar   52.32%      2,741,067      2.42      -0.63%  -2.16% -3.01%  2.40%            -0.75% -3.29%    -1.93%        -1.83%
50             Doom   49.10%      3,202,905      2.50      -3.57%   1.72% -0.74% -3.12%             0.03% -1.76%    -0.88%        -2.62%
78    Bounty Hunter   45.29%      4,146,758      2.17      -1.56%  -2.67% -1.43%  0.51%            -2.17% -3.33%    -2.19%         0.63%
92             Chen   44.34%        546,677      1.72      -0.27%  -0.51% -1.35% -0.81%            -0.24% -0.33%     0.03%        -1.33%
通过执行以下操作,我似乎能够将例如第1行分配给新data.frame:

newdataframe <- data.frame(Hero=subsetheroes[1,2],subsetheroes[1,6:ncol(subsetheroes)],stringsAsFactors = FALSE)
但是,我希望将包含百分比的行转换为原始数字,并将它们分配给新的数据帧。我认为这会奏效:

totals <- data.frame(Hero=subsetheroes[1,2],as.numeric(sub("%", "", subsetheroes[1,6:ncol(subsetheroes)])),stringsAsFactors = FALSE)
数字转换得很漂亮,但它使每个条目都成为一个新行,并重复列#1

为什么通过添加as.number()和sub()函数,这种行为会如此奇怪

编辑:我将生成表的代码放在这里:-subsetheroes表仅是使用subset()函数从heroestable中删除的几行。

尝试以下操作:

x <- subsetheroes[,c(2,6:ncol(subsetheroes))]
x[,2:ncol(x)] <- lapply(x[,2:ncol(x)],function(x) as.numeric(gsub(pattern="%","",x,fixed = TRUE)))

x我本打算向您展示一种更简单的方法,但是在示例数据中读取数据是一件非常麻烦的事情,因为您只显示了
head
的输出,而且您的列名中带有空格。因此,我所能做的就是建议您阅读
?data.frame
的值部分的第二段,以了解为什么这一行为一点也不奇怪。我之所以有奇怪的标题是因为我在上使用了readHTMLTable(),我没有命名列名。。。但是,如果有一种方法可以让它们成为单个单词,那么我很乐意……只需提供
dput(head(…)
的输出即可。嗨,Joran,这是个好主意。事实上,
dput(head(subtheroes))
工作得很好,但是
sub(“%”,”,dput(head(subtheroes))
似乎再次完全改变了格式,即使dput只输出ASCII,我正在sub():
[1]“c(\”,\”,“\”,“,“\”,“,”)“c”(“斯拉达”,“厄运”,“赏金猎人”,“陈”)[3]“c”(“52.32\”、“49.10%\”、“45.29%\”、“44.34%\”)“c”(“2741067\”、“3202905\”、“4146758\”、“546677\”)”
不,你误解了我。为了让人们有效地回答问题,我们需要使用示例数据。通常情况下,可以将示例数据复制并粘贴到
read.table(text=”“)
中,然后使用它,但由于列名太蠢,这不起作用。在您的问题中提供来自
dput
的输出,可以让试图帮助您的人轻松地读入R并与之合作。
       Hero as.numeric.sub..........subsetheroes.1..6.ncol.subsetheroes....
1   Slardar                                                           -0.63
2   Slardar                                                           -2.16
3   Slardar                                                           -3.01
4   Slardar                                                            2.40
x <- subsetheroes[,c(2,6:ncol(subsetheroes))]
x[,2:ncol(x)] <- lapply(x[,2:ncol(x)],function(x) as.numeric(gsub(pattern="%","",x,fixed = TRUE)))