我几乎无处不在,无法找到答案; R等价于Excel上的VLOOKUP.VLOOKUP允许我在整个列中查找特定值并将其应用于我的数据框的每一行.
在这种情况下,我想找到一个特定城市所在的国家(来自数据库)并在新列中返回该国家/地区的名称.
所以我有这个数据库:
countries <- c("UK", "US", "RUS")
cities <- c("LDN", "NY", "MOSC")
db <- cbind(countries, cities)
db
countries cities
[1,] "UK" "LDN"
[2,] "US" "NY"
[3,] "RUS" "MOSC"
Run Code Online (Sandbox Code Playgroud)
并且想要根据上面的数据库找到这些城市所在的国家(替换NA):
df
countries cities
[1,] NA "LDN"
[2,] NA "NY"
[3,] NA "MOSC"
Run Code Online (Sandbox Code Playgroud)
我完全不知道如何在R.
我想从推文中提取标签(推特句柄).
tweet <- "@me bla bla bla bla @him some text @her"
Run Code Online (Sandbox Code Playgroud)
附:
at <- regexpr('@[[:alnum:]]*', tweet)
handle <- substr(tweet,at+1,at+attr(at,"match.length")-1)
Run Code Online (Sandbox Code Playgroud)
我成功提取了第一个句柄
handle
[1] "me"
Run Code Online (Sandbox Code Playgroud)
但是我无法找到提取其他人的方法,有没有人知道这样做的方法?- 谢谢
我最近遇到了ggmap函数的问题 - 从来没有过.
我有这个data.frame:
> head(df)
longitude latitude freq
1 -118.7093 34.13446 2
2 -118.7092 34.13437 1
3 -118.6851 34.28574 5
4 -118.5986 34.26155 1
5 -118.5917 34.22762 6
6 -118.5914 34.37675 1
Run Code Online (Sandbox Code Playgroud)
我曾经能够使用get_map获取地图,然后使用ggmap绘制栅格对象的绘图.然后我会使用+ geom_point或其中任何一个来绘制我的数据,就像一个魅力.
map <- get_map(location=c(lon=-117.962813,lat=33.969399), zoom=10, maptype="hybrid")
finalmap <- ggmap(map, base_layer = ggplot(aes(x=longitude, y=latitude), data = df))
Run Code Online (Sandbox Code Playgroud)
但是现在ggmap返回:
Error in annotate("rect", xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax, :
unused arguments (xmin = xmin, xmax = xmax, ymin = ymin, ymax = …Run Code Online (Sandbox Code Playgroud) 我无法使用降序排序Rcpp
按升序排序:
NumericVector sortIt(NumericVector v){
std::sort(v.begin(), v.end());
return v;
}
Run Code Online (Sandbox Code Playgroud)
尝试按降序排序:
NumericVector sortIt(NumericVector v){
std::sort(v.begin(), v.end(), std::greater<int>()); // does not work returns ascending
return v;
}
Run Code Online (Sandbox Code Playgroud)
和
NumericVector sortIt(NumericVector v){
std::sort(numbers.rbegin(), numbers.rend()); // errors
return v;
}
Run Code Online (Sandbox Code Playgroud) 在updateTabsetPanelShiny 模块中调用时出现问题,没有它也能正常工作。
library(shiny)
mod_ui <- function(id){
ns <- NS(id)
tagList(
actionButton(ns("back"), "back")
)
}
mod <- function(input, output, session){
observeEvent(input$back, {
print("Button click, go back to home tab")
updateTabsetPanel(session = session, inputId = "tabs", selected = "home")
})
}
ui <- navbarPage(
"example",
id = "tabs",
tabPanel(
"home",
h4("updateTabsetPanel does not work with modules"),
h5("But the button below does"),
actionButton("switch", "switch")
),
tabPanel(
"secondtab",
mod_ui("second")
)
)
server <- function(input, output, session){
callModule(mod, "second")
observeEvent(input$switch, {
updateTabsetPanel(session = …Run Code Online (Sandbox Code Playgroud) 我有这个数据框
d f
"first tweet" A
"second tweet" B
"thrid tweet" C
Run Code Online (Sandbox Code Playgroud)
我想得到这个
d A B C
"first tweet" 1 0 0
"second tweet" 0 1 0
"thrid tweet" 0 0 1
Run Code Online (Sandbox Code Playgroud)
谢谢!
说我有以下内容 data.frame
df <- data.frame(letters = c("a, b", "a", "b", "a, c"), value = c(1, 2, 3, 4))
df
#> letters value
#> 1 a, b 1
#> 2 a 2
#> 3 b 3
#> 4 a, c 4
Run Code Online (Sandbox Code Playgroud)
我要拆分/融化到哪个
#> letters value
#> 1 a 1
#> 2 b 1
#> 3 a 2
#> 4 b 3
#> 5 a 4
#> 6 c 4
Run Code Online (Sandbox Code Playgroud)
为了每个单独记录 letters