为什么 Replace() 函数将我的数据帧转换为原子向量?

发布于 2025-01-13 02:59:59 字数 420 浏览 2 评论 0原文

我有这样的代码:

s12 <- replace(s12$nautos, s12$nautos == 11, 1) # this one does not produce error
s12 <- replace(s12$nautos, s12$nautos == 22, 2) # this one produces error that I cannot use $ because s12 is an atomic vector

再次运行它们都会产生错误。

当我查看全局环境时,s12 数据帧现在是一个向量。

为什么会发生这种情况?

nautos 列是一个包含 3000 个值的列,可以采用任意数字,但所有答案都是 0-4,除了两个条目 11 和 22,我认为这两个条目是拼写错误,应该是 1 和 2。

I have this code:

s12 <- replace(s12$nautos, s12$nautos == 11, 1) # this one does not produce error
s12 <- replace(s12$nautos, s12$nautos == 22, 2) # this one produces error that I cannot use $ because s12 is an atomic vector

Running them both again produces the error for both.

When I look at the global environment, s12 dataframe is now a vector.

Why did this happen?

column nautos is a column with 3000 values that takes any number, but all answers are 0-4 except for two entries that are 11 and 22, which I assume are typos and meant to be 1 and 2.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文