【发布时间】:2018-12-12 12:41:30
【问题描述】:
我对 R 非常陌生,并且遇到了数据框的问题。
所以我有一个看起来像这样的数据框 dataf:
# PlayerName playerValue
#5 Tammy Abraham 10,00 Mill. €
#6 Abdul Rahman Baba 8,00 Mill. €
#7 Mario Pasalic 8,00 Mill. €
#8 Lewis Baker 5,50 Mill. €
#9 Ola Aina 4,00 Mill. €
#10 Jamal Blackman 500 Th. €
然后我用这条线:
dataf$playerValue <- gsub(",", ".", gsub("[[:space:]].*", "", dataf$PlayerValue))
这个的输出是:
# PlayerName playerValue playerValue
#5 Tammy Abraham 10,00 Mill. € 10
#6 Abdul Rahman Baba 8,00 Mill. € 8
#7 Mario Pasalic 8,00 Mill. € 8
#8 Lewis Baker 5,50 Mill. € 5.5
#9 Ola Aina 4,00 Mill. € 4
#10 Jamal Blackman 500 Th. € 500
有没有办法让最终值从 500 变为 0.5?因为显然 500,000 小于 400 万,但这里的 int 500 将大于 4。
另外,我如何排除原始的 PlayerValue 列?当我运行我的代码时,它会打印出两次列,一次是最后的字符串和转换后的列。
感谢您的帮助。
【问题讨论】:
-
就打印两次而言,请注意
playerValue与PlayerValue不同。你有一个错字。至于你的其他问题,我没有看到任何使用ifelse()的捷径来检查"Th"的条件,但也许有人比我有更多的正则表达式专业知识。
标签: r