R:是否可以将向量的组合转化为数据集?

发布于 2025-01-07 04:01:14 字数 747 浏览 0 评论 0原文

我是 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 技术交流群。

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

发布评论

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

评论(2

來不及說愛妳 2025-01-14 04:01:14

您可能想要一个 data.frame 而不是 matrix (由 cbind 返回),

dataset <- data.frame(salary, ages, marSt)

而且,重复不是 sample() 的参数,您可能指的是 replace=TRUE。您最好阅读 R 简介。

You probably want a data.frame not a matrix (as returned by cbind),

dataset <- data.frame(salary, ages, marSt)

also, repeat is not an argument of sample(), you probably mean replace=TRUE. You would do well to read an introduction to R.

千里故人稀 2025-01-14 04:01:14

这可能会有所帮助:

salary = sample(2000:3000, 250, replace = T)
ages = sample(20:50, 250, replace = T)
marSt = sample(c("MARRIED", "SINGLE"), 250, replace = T)
# dataset = cbind(salary, ages, marSt) #WHAT YOU DID
dataset = data.frame(salary, ages, marSt) #WHAT YOU SHOULD HAVE DONE
data.reg = lm(salary~ages+marSt, data = dataset)

str() 还允许您查看对象的结构,以便您可以看到您所做的和我所做的之间的差异:

str(cbind(salary, ages, marSt))
str(data.frame(salary, ages, marSt))

输出:

> str(cbind(salary, ages, marSt))
 chr [1:250, 1:3] "2388" "2530" "2518" "2450" "2008" "2502" ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:3] "salary" "ages" "marSt"
> str(data.frame(salary, ages, marSt))
'data.frame':   250 obs. of  3 variables:
 $ salary: int  2388 2530 2518 2450 2008 2502 2264 2185 2207 2048 ...
 $ ages  : int  24 21 35 31 50 39 22 21 36 29 ...
 $ marSt : Factor w/ 2 levels "MARRIED","SINGLE": 1 2 2 2 2 2 2 1 1 2 ...

编辑:
巴蒂斯特在这个问题上击败了我,但我保留了我的答案,因为它补充了巴蒂斯特给出的解释

This may help:

salary = sample(2000:3000, 250, replace = T)
ages = sample(20:50, 250, replace = T)
marSt = sample(c("MARRIED", "SINGLE"), 250, replace = T)
# dataset = cbind(salary, ages, marSt) #WHAT YOU DID
dataset = data.frame(salary, ages, marSt) #WHAT YOU SHOULD HAVE DONE
data.reg = lm(salary~ages+marSt, data = dataset)

Also str() allows you to look at the structure of objects so you can see the difference between what you did and I did:

str(cbind(salary, ages, marSt))
str(data.frame(salary, ages, marSt))

Output:

> str(cbind(salary, ages, marSt))
 chr [1:250, 1:3] "2388" "2530" "2518" "2450" "2008" "2502" ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:3] "salary" "ages" "marSt"
> str(data.frame(salary, ages, marSt))
'data.frame':   250 obs. of  3 variables:
 $ salary: int  2388 2530 2518 2450 2008 2502 2264 2185 2207 2048 ...
 $ ages  : int  24 21 35 31 50 39 22 21 36 29 ...
 $ marSt : Factor w/ 2 levels "MARRIED","SINGLE": 1 2 2 2 2 2 2 1 1 2 ...

EDIT:
baptiste beat me to this one but I'm leaving my answer up as it adds to the explanation given by baptiste

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