[英]Create a column with a value from data frame in R
I have a dataset:我有一个数据集:
I want to create a column with the value from the date.我想用日期中的值创建一个列。 In this example, it is 201201, but the excel may have a different date.在此示例中,它是 201201,但 excel 可能具有不同的日期。 So I do not want to create a date by writing 201201, rather than create a link between the column and date.所以我不想通过写 201201 来创建日期,而不是在列和日期之间创建链接。 The output should look like:输出应如下所示:
I appreciate any assistance you can provide me.我很感激你能为我提供的任何帮助。
OK, so you have a data frame in R, similar to a worksheet in Excel.好的,所以您在 R 中有一个数据框,类似于 Excel 中的工作表。 I'll just create one for demonstration purposes:我将创建一个用于演示目的:
> data <- data.frame(Name=c("A","B","C"), books=c(3,5,8))
> data
Name books
1 A 3
2 B 5
3 C 8
Now I will create a scalar of class "Date", similar to the value in your cell D1:现在我将创建一个“Date”类的标量,类似于单元格 D1 中的值:
> Date <- as.Date("2001-12-20")
> Date
[1] "2001-12-20"
Now I will create a new column in data corresponding to this date:现在我将在与此日期对应的数据中创建一个新列:
> data$Date <- Date
> data
Name books Date
1 A 3 2001-12-20
2 B 5 2001-12-20
3 C 8 2001-12-20
Is that what you had in mind?那是你想的吗?
Edit : To import the date value from only cell D1 in an Excel file called "Report.xlsx" (
to automatize
automaterize
to make the process run with less human intervention):编辑:仅从名为“Report.xlsx”的 Excel 文件中的单元格 D1 导入日期值(
自动化
自动化
,使流程以较少的人为干预运行):
> library(readxl)
> Report <- read_excel("E:/Book1.xlsx", range="D1", col_names="today", col_types = "text")
> Report %>% mutate(today=as.Date(today, format="%y%m%d"))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.