我有一个两个变量的数据帧,其中一个是字符向量。"MyVector" 中的每一行都包含一个只有一个名字的字符串(即“Pete”)。名称在字符串中的位置可能会有所不同。我想创建将列表中的名称与字符串中的名称匹配的代码,并将该名称提取到数据框中的新变量中。如果名称始终位于向量“MyVector”中的相同位置,我将创建一个新变量作为 MyVector 的子字符串,将名称提取到新列中。我从 Stringr 尝试了各种版本的 str_detect 无济于事。
挑战:如果名称位于多个位置,我如何检测名称或将名称提取到新变量中并将其放入 MyDF?
#Create the data frame
var.1 <-rep(c(1,5,3),2)
MyVector <- c("I know Pete", "Jerry has a new job","Victor is an employee","How to work with Pete","Too Many Students","Bob is mean")
MyDF <-as.data.frame(cbind(var.1,MyVector))
#Create a vector of a list of names I want to extract into a new column in the dataframe.
Extract <- c("Jerry","Pete", "Bob", "Victor")
#Match would be perfect if I could use it on character vectors
MyDF$newvar <-match(MyDF$MyVector,Extract)
Run Code Online (Sandbox Code Playgroud)
我的最终 data.frame 应该类似于下面的输出。 …
谁可以帮忙,这是我的第一篇文章.我花了几个小时试图弄清楚如何使用ggplot为我想要创建的闪亮应用程序生成条形图.然而,ui作品找到了; 服务器功能生成一个空图.问题在于renderPlot函数.我相信我不能将反应值正确地传递给ggplot中的aes_string参数.C2是过滤的数据集.目标是构建一个简单的应用程序,用户在其中选择两个变量,基于这些变量过滤数据集.子集化数据集将传递给ggplot数据参数.
library(shiny)
library(dplyr)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
selectInput(inputId = "Demog",label = "Factor:",choices = c("HH Income" = "Income",
"Age Group" = "Age",
"US Region" = "Region") , selected = "Age"),
selectInput(inputId = "Car",label = "VW Model:",choices = c("BEETLE" = "BEETLE",
"CC" = "CC",
"EOS" = "EOS",
"GOLF" = "GOLF",
"GTI" ="GOLF SPORTSWAGEN GTI",
"JETTA" = "JETTA",
"PASSAT" = "PASSAT",
"TIGUAN" = "TIGUAN",
"TOUAREG" = "TOUAREG") , selected = "BEETLE"),
radioButtons(inputId = "Metric",label ="Measurement Type",choices =
c("Conquest Volume Index" = …Run Code Online (Sandbox Code Playgroud)