如何更改特定列的值格式-R

发布于 2025-01-23 23:25:47 字数 1394 浏览 2 评论 0 原文

假设我有一个数据集流量 column coper_count 显示每个流量计数站的流量计数:

triffate_counting_counting_station_id agript_count_count
1 24.592
2 65.500
3 4.976

问题是<代码是<代码 traffil_count 被解释为浮点类型,而值应表示整数数字。 As an example, when I generate a new column Traffic_count_TimesTen which is formulated as traffic$Traffic_count*10, the resulting table is:

Traffic_counting_station_ID Traffic_count Traffic_count_TimesTen
1 24.592 245.92
2 65.500 655.00
3 4.976 49.76

当我应用流量$ clumag_count&lt; - as.integer(流量$ triffum_count) code> traffum_count 的值分别为25、66和5。

应用流量$ crivical_count&lt; - as.numeric(gsub(“。”,“”,traffic $ crivate_count))删除定义类型float的点,导致 na

我如何将流量_Count的值转换为整数数字,从而将 clockugh_count 中的值视为24592、65500、4976,而 code> cropaile> cropail_count_timesten 中的值则被视为245920,655000和49760?

Suppose that I have a dataset traffic with a column Traffic_count that displays the traffic count for each traffic counting station:

Traffic_counting_station_ID Traffic_count
1 24.592
2 65.500
3 4.976

The problem is that Traffic_count is interpreted as a float type while the values should represent integer numbers. As an example, when I generate a new column Traffic_count_TimesTen which is formulated as traffic$Traffic_count*10, the resulting table is:

Traffic_counting_station_ID Traffic_count Traffic_count_TimesTen
1 24.592 245.92
2 65.500 655.00
3 4.976 49.76

When I apply traffic$Traffic_count <- as.integer(traffic$Traffic_count) the values for Traffic_count will just be 25, 66 and 5 respectively.

Applying traffic$Traffic_count <- as.numeric(gsub(".","",traffic$Traffic_count)) to remove the point that defines the type float, results in NA.

How can I convert the values of Traffic_count to integer numbers so that the values in Traffic_count are regarded as 24592, 65500, 4976 and the values in Traffic_count_TimesTen are regarded as 245920, 655000 and 49760?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

早茶月光 2025-01-30 23:25:47

您的 gsub 解决方案几乎是正确的。尝试以下内容:

traffic$Traffic_count <- as.numeric(gsub("\\.","",traffic$Traffic_count))

说明

是REGEX中的一个特殊标志,意思是“任何”字符,因此它将用替换每个字母“” 。如果您想更换点,则需要在其前面放置 \ 。但是,当R试图解释字符串中的单个“”时,我们需要告诉R本身,不要用另一个斜杠进行预处理。因此 \\

your gsub solution is almost correct.Try the following:

traffic$Traffic_count <- as.numeric(gsub("\\.","",traffic$Traffic_count))

Explanation

. is a special sign in regex, meaning "any" character, therefore it will replace every letter with "". If you exlicitly want to replace a dot, you need to put a \ in front of it. But as R tries to interpret a single "" within a string we need to tell R itself not to preprocess the slash with another slash. therefore \\

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文