R:是否可以将向量的组合转化为数据集?
我是 R 的初学者。
在观看了一些有关回归分析的教程(在 youtube 上)后,我决定构建自己的数据集并将我学到的知识应用到其中。这就是我所做的!
我想随机创建一个工资、年龄和婚姻状况的列表。
Salaries
salary = sample(2000:3000, 250, replace = T)
Ages
ages = sample(20:50, 250, replace = T)
MaritalStatus
marSt = sample(c("MARRIED", "SINGLE"), 250, repeat = T)
然后,我将三组数据与:
dataset = cbind(salary, ages, marSt)
最后,我尝试使用以下命令对我认为是我的新数据集进行回归:
data.reg = lm(salary~ages+marSt, data = dataset)
... 只是为了告诉我有一个错误,并且对象“数据集”实际上不是数据集。
我的问题有两个: (i) 是否可以通过向量组合创建数据集? (ii) 如果没有,R 中是否有任何方法可以创建数据集而无需从其他来源导入数据?
非常感谢您,我是初学者,请不要在您的回答中过于复杂。
I am a beginner in R.
After watching a number of tutorials on regression analysis (on youtube), I decided to make up my own data set and apply what I learnt to it. This is what I did!
I wanted to randomly create a list of salaries, ages and marital status.
Salaries
salary = sample(2000:3000, 250, replace = T)
Ages
ages = sample(20:50, 250, replace = T)
MaritalStatus
marSt = sample(c("MARRIED", "SINGLE"), 250, repeat = T)
Then, I combined the three sets of data with:
dataset = cbind(salary, ages, marSt)
Finally, I tried to run a regression on what I thought was my new data set with this command:
data.reg = lm(salary~ages+marSt, data = dataset)
... only for me to be told that there was an error and that the object "dataset" was actually NOT a dataset.
My question is two fold:
(i) Is it possible to create data sets from combinations of vectors?
(ii) If no, is there any way in R to create data sets without importing them from other sources?
Thank you very much and please I am a beginner and do not be too sophisticated in your response.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能想要一个
data.frame
而不是matrix
(由cbind
返回),而且,重复不是
sample()
的参数,您可能指的是replace=TRUE
。您最好阅读 R 简介。You probably want a
data.frame
not amatrix
(as returned bycbind
),also, repeat is not an argument of
sample()
, you probably meanreplace=TRUE
. You would do well to read an introduction to R.这可能会有所帮助:
str() 还允许您查看对象的结构,以便您可以看到您所做的和我所做的之间的差异:
输出:
编辑:
巴蒂斯特在这个问题上击败了我,但我保留了我的答案,因为它补充了巴蒂斯特给出的解释
This may help:
Also str() allows you to look at the structure of objects so you can see the difference between what you did and I did:
Output:
EDIT:
baptiste beat me to this one but I'm leaving my answer up as it adds to the explanation given by baptiste