lm_model 添加的意外符号错误 [英] Unexpected symbol error for lm_model addition

查看:25
本文介绍了lm_model 添加的意外符号错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试运行此代码:

I've been trying to run this code:

lm_model <- lm(Calories ~ Sodium + Carbohydrates + Protein + Caffeine + Dietary Fiber, 
               data = Starbucks)

但我一直在接受

错误:lm_model <-lm(卡路里~钠+碳水化合物+蛋白质+咖啡因+膳食纤维"中的意外符号

Error: unexpected symbol in "lm_model <- lm(Calories ~ Sodium + Carbohydrates + Protein + Caffeine + Dietary Fiber"

推荐答案

你会遇到这个问题,因为列名Dietary Fiber"是两个词

You're getting that issue because the column name 'Dietary Fiber' is two words

要修复它,请准确找出它在您的数据中的名称(例如,运行 colnames(Starbucks))并查找那里的列并确保您将其命名为完全相同

To fix it, find out exactly what it's called in your data (e.g. run colnames(Starbucks)) and look for the column there and make sure you name it exactly the same

如果 Dietary Fiber 列名确实包含空格,您可以用反引号字符(即 ` )将其括起来,以便 R 知道将其视为单个列名

If the Dietary Fiber column name does indeed contain a space, you can surround it with the backtick character (i.e. ` ) so R knows to treat it as one single column name

这是一个将 lm() 与名称包含空格的列一起使用的示例

Here's an example of using lm() with a column whose names contain a space

a <- iris[1:10, c(1:2)]

# This works
lm(Sepal.Length ~ Sepal.Width, data = a)

# But what if we try the same thing with a space in the column name?
colnames(a)[2] <- "Sepal Width"

# Now it errors
lm(Sepal.Length ~ Sepal.Width, data = a)
# Error in eval(predvars, data, env) : object 'Sepal.Width' not found

# To use lm() function with a column name that contains spaces, use the backtick
lm(Sepal.Length ~ `Sepal Width`, data = a)

这篇关于lm_model 添加的意外符号错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆