将字符串转换为 RGB,然后绘制它
Converting string to RGB, then plot it
我在 R 中解决作业时遇到问题。
基本上,我得到了一个字符串,这里是生日日期,应该以某种方式将其转换为数值作为 rgb
函数的输入。 在我之前太隐晦了,请阅读作业说明:
Given a string with a birthday such as the one shown below, create a plot showing that person’s birthday color.
The birthday color is defined by using the day of month, the month, and the year as indexes for the rgb() function. The day of month indicates how much red we want in the color, the month the green, and the year expressed the blue. You can assume that all months have 31 days. With respect to the years, assume that everyone who is going to use the birthday color function is born in or after 1915. Below you can see that my birthday color is a blueish green, and that of my son is a greenish blue.
Hint: one of the first steps should be to split the string into separate elements, and to make sure that these are subsequently converted into numeric values.
birthday <- "02-10-1972"
calcBirthdayColor <- function(bd) {
## your code here
}
plot(0,type="n",
axes=FALSE,ylab="",xlab="",
xlim=c(-10,10),ylim=c(-10,10),asp=1)
## My birthday color:
rect(-10,-6,10,6,col=calcBirthdayColor(birthday))
## And the color of my son:
rect(-6,-10,6,10,col=calcBirthdayColor("10-09-2013"))
此外还提出了以下问题:
http://i.imgur.com/wrkfhMQ.png
我最初的猜测是,在转换为数字后,像 '01-10-1972' 这样的出生日期字符串可以插入到 rgb
中,但这实际上不起作用,因为 maxColorValue
是错误的,例如RGB(1,10,1972)
即使我将它更改为大于 1972 的值(例如 1,10,1972,2015),我也无法获得所提供矩形的近似颜色。
如何使出生日期格式适合 rgb
函数?
非常感谢您的任何提示。
为了好玩:
calcBirthdayColor <- function(birthday) {
do.call(rgb, as.list(setNames(
(as.numeric(strsplit(birthday, "-")[[1]]) / c(31, 12, 1) -
c(0, 0, 1915) ) /
c(1, 1, 2015-1915),
c("red", "green", "blue"))))}
plot(0, type="n",
axes=FALSE,ylab="",xlab="",
xlim=c(-10,10),ylim=c(-10,10),asp=1)
## My birthday color:
rect(-10,-6,10,6,col=calcBirthdayColor(birthday))
## And the color of my son:
rect(-6,-10,6,10,col=calcBirthdayColor("10-09-2013"))
# red
rect(-3,-3,3,3,col=calcBirthdayColor("31-01-1915"))
非常感谢!现在,我明白了……虽然花了我一段时间……我是新手。
同样,我今天在 Uni 的另一个 R class 中编写了一个解决方案。看起来不如上面那个优雅,但工作起来一样好。
感谢您的帮助!
birthday <- "02-10-1972"
calcBirthdayColor <- function(birthday) {
bd.colour <- rgb(as.numeric(strsplit(birthday, "-")[[1]])[1]/31,
as.numeric(strsplit(birthday, "-")[[1]])[2]/12,
((as.numeric(strsplit(birthday, "-")[[1]])[3])-1915) /100)
plot(0,type="n",
axes=FALSE,ylab="",xlab="",
xlim=c(-10,10),ylim=c(-10,10),asp=1)
rect(-10,-6,10,6, col=bd.colour)
}
calcBirthdayColor(birthday)
我在 R 中解决作业时遇到问题。
基本上,我得到了一个字符串,这里是生日日期,应该以某种方式将其转换为数值作为 rgb
函数的输入。 在我之前太隐晦了,请阅读作业说明:
Given a string with a birthday such as the one shown below, create a plot showing that person’s birthday color.
The birthday color is defined by using the day of month, the month, and the year as indexes for the rgb() function. The day of month indicates how much red we want in the color, the month the green, and the year expressed the blue. You can assume that all months have 31 days. With respect to the years, assume that everyone who is going to use the birthday color function is born in or after 1915. Below you can see that my birthday color is a blueish green, and that of my son is a greenish blue.
Hint: one of the first steps should be to split the string into separate elements, and to make sure that these are subsequently converted into numeric values.
birthday <- "02-10-1972"
calcBirthdayColor <- function(bd) {
## your code here
}
plot(0,type="n",
axes=FALSE,ylab="",xlab="",
xlim=c(-10,10),ylim=c(-10,10),asp=1)
## My birthday color:
rect(-10,-6,10,6,col=calcBirthdayColor(birthday))
## And the color of my son:
rect(-6,-10,6,10,col=calcBirthdayColor("10-09-2013"))
此外还提出了以下问题:
http://i.imgur.com/wrkfhMQ.png
我最初的猜测是,在转换为数字后,像 '01-10-1972' 这样的出生日期字符串可以插入到 rgb
中,但这实际上不起作用,因为 maxColorValue
是错误的,例如RGB(1,10,1972)
即使我将它更改为大于 1972 的值(例如 1,10,1972,2015),我也无法获得所提供矩形的近似颜色。
如何使出生日期格式适合 rgb
函数?
非常感谢您的任何提示。
为了好玩:
calcBirthdayColor <- function(birthday) {
do.call(rgb, as.list(setNames(
(as.numeric(strsplit(birthday, "-")[[1]]) / c(31, 12, 1) -
c(0, 0, 1915) ) /
c(1, 1, 2015-1915),
c("red", "green", "blue"))))}
plot(0, type="n",
axes=FALSE,ylab="",xlab="",
xlim=c(-10,10),ylim=c(-10,10),asp=1)
## My birthday color:
rect(-10,-6,10,6,col=calcBirthdayColor(birthday))
## And the color of my son:
rect(-6,-10,6,10,col=calcBirthdayColor("10-09-2013"))
# red
rect(-3,-3,3,3,col=calcBirthdayColor("31-01-1915"))
非常感谢!现在,我明白了……虽然花了我一段时间……我是新手。
同样,我今天在 Uni 的另一个 R class 中编写了一个解决方案。看起来不如上面那个优雅,但工作起来一样好。
感谢您的帮助!
birthday <- "02-10-1972"
calcBirthdayColor <- function(birthday) {
bd.colour <- rgb(as.numeric(strsplit(birthday, "-")[[1]])[1]/31,
as.numeric(strsplit(birthday, "-")[[1]])[2]/12,
((as.numeric(strsplit(birthday, "-")[[1]])[3])-1915) /100)
plot(0,type="n",
axes=FALSE,ylab="",xlab="",
xlim=c(-10,10),ylim=c(-10,10),asp=1)
rect(-10,-6,10,6, col=bd.colour)
}
calcBirthdayColor(birthday)