【发布时间】:2020-03-26 11:34:21
【问题描述】:
你们中的一些人这可能是一个简单的问题。
我有 2 个数据框:
dput(head(Activitieslessthan35))
structure(list(`Main job: Working time in main job` = c(470,
440, 430, 430, 410, 150), Sleep = c(420, 450, 450, 420, 450,
460), `Unspecified TV video or DVD watching` = c(60, 40, 210,
190, 60, 0), Eating = c(80, 60, 40, 70, 60, 130), `Other personal care:Wash and dress` = c(60,
60, 50, 50, 70, 50), `Travel to work from home and back only` = c(60,
60, 50, 90, 90, 30), `Unspecified radio listening` = c(140, 180,
50, 90, 140, 160), `Other specified social life` = c(350, 270,
310, 330, 710, 440), `Socialising with family` = c(350, 270,
360, 330, 730, 540), `Food preparation and baking` = c(410, 310,
420, 380, 1000, 950)), row.names = c(NA, 6L), class = "data.frame")
和
dput(head(ActivitiesMoreOrEqual35))
structure(list(`Main job: Working time in main job` = c(360,
420, 390, 490, 540, 390), Sleep = c(590, 480, 310, 560, 280,
370), `Unspecified TV video or DVD watching` = c(100, 60, 130,
120, 60, 30), Eating = c(70, 100, 70, 40, 190, 80), `Other personal care:Wash and dress` = c(10,
30, 100, 60, 270, 90), `Travel to work from home and back only` = c(0,
50, 260, 50, 0, 0), `Unspecified radio listening` = c(50, 80,
260, 80, 210, 200), `Other specified social life` = c(190, 320,
790, 250, 580, 420), `Travel in the course of work` = c(50, 80,
260, 70, 120, 200), `Food preparation and baking` = c(440, 570,
820, 570, 820, 590)), row.names = c(NA, 6L), class = "data.frame")
我想将 data.frames 转换为因子 - 例如,有一个名为 Activitieslessthan35 的因子变量,其中包含要用作级别的数据框的列,例如“主要工作:主要工作的工作时间”, “睡眠”等。稍后我还想在并排条形图上绘制(总和)因素的水平。
我不知道您是否关心将 data.frame 转换为因子变量以及如何更改 data.frames 的格式以创建绘图
欢迎提出建议
【问题讨论】:
-
data.frame 的定义是
list,其中列表的各个元素必须具有相同的长度,但可以与不同的class不同,例如(整数、数字、字符、因子等)。我会尝试改写您的问题并具体添加您想要的新data.frame,然后我相信人们可以给您一个如何通过R代码生成新data.frame的示例