使用数据框中的行值选择第二个数据框中的列
Using row value in dataframe to choose column in second dataframe
所以我有两个数据框...一个是宽格式,其中列名是日期,日期下列出的是那几天发生的所有问题。第二个数据框具有各种列,描述了当天发生的事情的其他信息。像这样:
df_1 <- 2017-07-15 2017-08-15 2017-09-15 2017-10-15
'crashed' 'crashed' 'reset' 'crashed'
'damaged' 'reset' 'reset' 'reset'
'no problems' 'crashed' 'crashed' 'reset'
df_2 <- Date Make Model Color
2017-07-15 iPhone 7 black
2017-08-15 Android Galaxy silver
2017-09-15 iPhone 6 white
2017-10-15 Blackberry Curve black
我想访问 df_1 列中的所有数据,谁的名字与 df_2 的日期相匹配...原因是因为我正在尝试使用来自 df_2 用于 ggplot geom_point()
并在光标悬停在同一日期
上时在工具提示中显示来自 df_1 的信息
(以下代码取自此处,简单地输入我的数据框):
https://gitlab.com/snippets/16220
library("shiny")
library("ggplot2")
ui <- pageWithSidebar(
headerPanel("Tooltips in ggplot2 + shiny"),
sidebarPanel(
HTML("Tooltips are managed by combination of shiny+ggplot hover functionality",
"and css styles. By setting hover argument of 'plotOutput' we could access",
"hover data from the server side, as an ordinary input. Hover input is",
"a list with: position of cursor ON the image; domain - that is",
"values of variables at the plotting area edges; range - that is position",
"of plotting area edges in pixels relative to whole image element.",
"Additionally for ggplot used mappings are returned. </br>",
"To create tooltip first we need to identify position of the cursor",
"inside the image element. We do it by calculating distances from left and",
"top edge of image element from hover data. Then we create tooltip, in this",
"app it is 'wellPanel' with some info inside, and set 'position' property",
"to 'absolute' and set 'left' and 'top' properties to calculated values.",
"However, 'absolute' position is defined as relative to the nearest positioned",
"ancestor. Because we want to position tooltip inside the image, we need",
"to put both 'plotOutput' with image and 'uiOutput' with tooltip content",
"inside additional 'div' element with 'position' property set to 'relative'.",
"We don't set top, left etc. for this element, so the actual position of",
"the image doesn't change - it's edges are identical as previously, so",
"we can use 'div' (for positioning tooltip) as substitute for image. </br>"),
width = 3
),
mainPanel(
# this is an extra div used ONLY to create positioned ancestor for tooltip
# we don't change its position
div(
style = "position:relative",
plotOutput("scatterplot",
hover = hoverOpts("plot_hover", delay = 100, delayType = "debounce")),
uiOutput("hover_info")
),
width = 7
)
)
server <- function(input, output) {
output$scatterplot <- renderPlot({
ggplot(df_2, aes(x = Date, y = make)) +
geom_point()
})
output$hover_info <- renderUI({
hover <- input$plot_hover
point <- nearPoints(df_2, hover, threshold = 5, maxpoints = 1, addDist = TRUE)
if (nrow(point) == 0) return(NULL)
# calculate point position INSIDE the image as percent of total dimensions
# from left (horizontal) and from top (vertical)
left_pct <- (hover$x - hover$domain$left) / (hover$domain$right - hover$domain$left)
top_pct <- (hover$domain$top - hover$y) / (hover$domain$top - hover$domain$bottom)
# calculate distance from left and bottom side of the picture in pixels
left_px <- hover$range$left + left_pct * (hover$range$right - hover$range$left)
top_px <- hover$range$top + top_pct * (hover$range$bottom - hover$range$top)
# create style property fot tooltip
# background color is set so tooltip is a bit transparent
# z-index is set so we are sure are tooltip will be on top
style <- paste0("position:absolute; z-index:100; background-color: rgba(245, 245, 245, 0.85); ",
"left:", left_px + 2, "px; top:", top_px + 2, "px;")
# actual tooltip created as wellPanel
wellPanel(
style = style,
p(HTML(paste0(DF_1 DATE THAT MATCHES DF_2 DATE)))
)
})
}
runApp(list(ui = ui, server = server))
这个适合你吗?
数据
df_1 <- read.table(text="2017-07-15 2017-08-15 2017-09-15 2017-10-15
'crashed' 'crashed' 'reset' 'crashed'
'damaged' 'reset' 'reset' 'reset'
'no problems' 'crashed' 'crashed' 'reset'",header=TRUE,stringsAsFactors = FALSE)
df_2 <- read.table(text="Date Make Model Color
2017-07-15 iPhone 7 black
2017-08-15 Android Galaxy silver
2017-09-15 iPhone 6 white
2017-10-15 Blackberry Curve black",header=TRUE,stringsAsFactors = FALSE)
代码
library(dplyr)
library(tidyr)
df_1 %>%
mutate(variable = letters[1:n()]) %>%
gather(Date, val, 1:4) %>%
mutate(Date = gsub("\.","-",substr(Date,2,nchar(Date)))) %>% # you may not have to do this with your actual data
rbind(df_2 %>% gather(variable,val,-1)) %>%
spread(Date,val)
结果
# variable 2017-07-15 2017-08-15 2017-09-15 2017-10-15
# 1 a crashed crashed reset crashed
# 2 b damaged reset reset reset
# 3 c no problems crashed crashed reset
# 4 Color black silver white black
# 5 Make iPhone Android iPhone Blackberry
# 6 Model 7 Galaxy 6 Curve
好的,这是我的解决方法,虽然它可能不是最好的?似乎是我唯一能做的事情来得到我想要的东西。感谢 MOODY_MUDSKIPPER 的起点!!
df_1 %>%
mutate(variable = letters[1:n()])
df_1 <- melt(df_1, id.vars='variable')
colnames(df_1)[2] <- "Date" #because the newly created variable column and the column with the dates were both named 'variable'...
df_1 <- dcast(df_1, Date ~ variable)
df_2 <- cbind(df_2,df_1,[,-1]) #subsetting to remove the first column from df_1 so that there arent two columns with Dates in them
现在 df_2 将组织 df_1 的所有信息
所以我有两个数据框...一个是宽格式,其中列名是日期,日期下列出的是那几天发生的所有问题。第二个数据框具有各种列,描述了当天发生的事情的其他信息。像这样:
df_1 <- 2017-07-15 2017-08-15 2017-09-15 2017-10-15
'crashed' 'crashed' 'reset' 'crashed'
'damaged' 'reset' 'reset' 'reset'
'no problems' 'crashed' 'crashed' 'reset'
df_2 <- Date Make Model Color
2017-07-15 iPhone 7 black
2017-08-15 Android Galaxy silver
2017-09-15 iPhone 6 white
2017-10-15 Blackberry Curve black
我想访问 df_1 列中的所有数据,谁的名字与 df_2 的日期相匹配...原因是因为我正在尝试使用来自 df_2 用于 ggplot geom_point()
并在光标悬停在同一日期
(以下代码取自此处,简单地输入我的数据框): https://gitlab.com/snippets/16220
library("shiny")
library("ggplot2")
ui <- pageWithSidebar(
headerPanel("Tooltips in ggplot2 + shiny"),
sidebarPanel(
HTML("Tooltips are managed by combination of shiny+ggplot hover functionality",
"and css styles. By setting hover argument of 'plotOutput' we could access",
"hover data from the server side, as an ordinary input. Hover input is",
"a list with: position of cursor ON the image; domain - that is",
"values of variables at the plotting area edges; range - that is position",
"of plotting area edges in pixels relative to whole image element.",
"Additionally for ggplot used mappings are returned. </br>",
"To create tooltip first we need to identify position of the cursor",
"inside the image element. We do it by calculating distances from left and",
"top edge of image element from hover data. Then we create tooltip, in this",
"app it is 'wellPanel' with some info inside, and set 'position' property",
"to 'absolute' and set 'left' and 'top' properties to calculated values.",
"However, 'absolute' position is defined as relative to the nearest positioned",
"ancestor. Because we want to position tooltip inside the image, we need",
"to put both 'plotOutput' with image and 'uiOutput' with tooltip content",
"inside additional 'div' element with 'position' property set to 'relative'.",
"We don't set top, left etc. for this element, so the actual position of",
"the image doesn't change - it's edges are identical as previously, so",
"we can use 'div' (for positioning tooltip) as substitute for image. </br>"),
width = 3
),
mainPanel(
# this is an extra div used ONLY to create positioned ancestor for tooltip
# we don't change its position
div(
style = "position:relative",
plotOutput("scatterplot",
hover = hoverOpts("plot_hover", delay = 100, delayType = "debounce")),
uiOutput("hover_info")
),
width = 7
)
)
server <- function(input, output) {
output$scatterplot <- renderPlot({
ggplot(df_2, aes(x = Date, y = make)) +
geom_point()
})
output$hover_info <- renderUI({
hover <- input$plot_hover
point <- nearPoints(df_2, hover, threshold = 5, maxpoints = 1, addDist = TRUE)
if (nrow(point) == 0) return(NULL)
# calculate point position INSIDE the image as percent of total dimensions
# from left (horizontal) and from top (vertical)
left_pct <- (hover$x - hover$domain$left) / (hover$domain$right - hover$domain$left)
top_pct <- (hover$domain$top - hover$y) / (hover$domain$top - hover$domain$bottom)
# calculate distance from left and bottom side of the picture in pixels
left_px <- hover$range$left + left_pct * (hover$range$right - hover$range$left)
top_px <- hover$range$top + top_pct * (hover$range$bottom - hover$range$top)
# create style property fot tooltip
# background color is set so tooltip is a bit transparent
# z-index is set so we are sure are tooltip will be on top
style <- paste0("position:absolute; z-index:100; background-color: rgba(245, 245, 245, 0.85); ",
"left:", left_px + 2, "px; top:", top_px + 2, "px;")
# actual tooltip created as wellPanel
wellPanel(
style = style,
p(HTML(paste0(DF_1 DATE THAT MATCHES DF_2 DATE)))
)
})
}
runApp(list(ui = ui, server = server))
这个适合你吗?
数据
df_1 <- read.table(text="2017-07-15 2017-08-15 2017-09-15 2017-10-15
'crashed' 'crashed' 'reset' 'crashed'
'damaged' 'reset' 'reset' 'reset'
'no problems' 'crashed' 'crashed' 'reset'",header=TRUE,stringsAsFactors = FALSE)
df_2 <- read.table(text="Date Make Model Color
2017-07-15 iPhone 7 black
2017-08-15 Android Galaxy silver
2017-09-15 iPhone 6 white
2017-10-15 Blackberry Curve black",header=TRUE,stringsAsFactors = FALSE)
代码
library(dplyr)
library(tidyr)
df_1 %>%
mutate(variable = letters[1:n()]) %>%
gather(Date, val, 1:4) %>%
mutate(Date = gsub("\.","-",substr(Date,2,nchar(Date)))) %>% # you may not have to do this with your actual data
rbind(df_2 %>% gather(variable,val,-1)) %>%
spread(Date,val)
结果
# variable 2017-07-15 2017-08-15 2017-09-15 2017-10-15
# 1 a crashed crashed reset crashed
# 2 b damaged reset reset reset
# 3 c no problems crashed crashed reset
# 4 Color black silver white black
# 5 Make iPhone Android iPhone Blackberry
# 6 Model 7 Galaxy 6 Curve
好的,这是我的解决方法,虽然它可能不是最好的?似乎是我唯一能做的事情来得到我想要的东西。感谢 MOODY_MUDSKIPPER 的起点!!
df_1 %>%
mutate(variable = letters[1:n()])
df_1 <- melt(df_1, id.vars='variable')
colnames(df_1)[2] <- "Date" #because the newly created variable column and the column with the dates were both named 'variable'...
df_1 <- dcast(df_1, Date ~ variable)
df_2 <- cbind(df_2,df_1,[,-1]) #subsetting to remove the first column from df_1 so that there arent two columns with Dates in them
现在 df_2 将组织 df_1 的所有信息