得到“NA”当我运行标准差时

发布于 2024-11-03 04:55:42 字数 218 浏览 0 评论 0原文

快问。我将 csv 文件读入变量 data 中。它有一个列标签 var,其中包含数值。

当我运行命令时,

sd(data$var)

我得到的

[1] NA 

不是标准差。

你能帮我弄清楚我做错了什么吗?

Quick question. I read my csv file into the variable data. It has a column label var, which has numerical values.

When I run the command

sd(data$var)

I get

[1] NA 

instead of my standard deviation.

Could you please help me figure out what I am doing wrong?

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

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

发布评论

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

评论(4

趁年轻赶紧闹 2024-11-10 04:55:42

尝试 sd(data$var, na.rm=TRUE) ,然后 var 列中的任何 NA 将被忽略。还将付费检查您的数据,以确保 NA 应该是 NA,并且没有读取错误,例如 head(data)tail(data) 等命令> 和 str(data) 应该对此有所帮助。

Try sd(data$var, na.rm=TRUE) and then any NAs in the column var will be ignored. Will also pay to check out your data to make sure the NA's should be NA's and there haven't been read in errors, commands like head(data), tail(data), and str(data) should help with that.

救星 2024-11-10 04:55:42

我曾经犯过一两次在 dplyr 字符串中重复使用变量名的错误,这导致了问题。

mtcars %>%
  group_by(gear) %>%
  mutate(ave = mean(hp)) %>%
  ungroup() %>%
  group_by(cyl) %>%
  summarise(med = median(ave),
            ave = mean(ave), # should've named this variable something different
            sd = sd(ave)) # this is the sd of my newly created variable "ave", not the original one.

I've made the mistake a time or two of reusing variable names in dplyr strings which has caused issues.

mtcars %>%
  group_by(gear) %>%
  mutate(ave = mean(hp)) %>%
  ungroup() %>%
  group_by(cyl) %>%
  summarise(med = median(ave),
            ave = mean(ave), # should've named this variable something different
            sd = sd(ave)) # this is the sd of my newly created variable "ave", not the original one.
哑剧 2024-11-10 04:55:42

您可能在 var 中缺少值,或者该列不是数字,或者只有一行。

尝试删除缺失值,这将有助于第一种情况:

sd(dat$var, na.rm = TRUE)

如果这不起作用,请检查它

class(dat$var)

是否为“数字”(第二种情况)并且

nrow(dat)

是否大于 1(第三种情况)。

最后,data 是 R 中的一个函数,因此最好使用不同的名称,我在这里已经这样做了。

You probably have missing values in var, or the column is not numeric, or there's only one row.

Try removing missing values which will help for the first case:

sd(dat$var, na.rm = TRUE)

If that doesn't work, check that

class(dat$var)

is "numeric" (the second case) and that

nrow(dat)

is greater than 1 (the third case).

Finally, data is a function in R so best to use a different name, which I've done here.

亣腦蒛氧 2024-11-10 04:55:42

数据中可能存在 Inf-Inf 作为值。

尝试

is.finite(data)

min(data, na.rm = TRUE)
max(data, na.rm = TRUE)

检查一下情况是否确实如此。

There may be Inf or -Inf as values in the data.

Try

is.finite(data)

or

min(data, na.rm = TRUE)
max(data, na.rm = TRUE)

to check if that is indeed the case.

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