我在变量中有一个字符串,我们称之为v1.该字符串表示图片编号,采用"Pic 27 + 28"的形式.我想提取第一个数字并将其存储在一个名为item的新变量中.
我尝试过的一些代码是:
item <- unique(na.omit(as.numeric(unlist(strsplit(unlist(v1),"[^0-9]+")))))
Run Code Online (Sandbox Code Playgroud)
这很好,直到我找到了一个列表:
[1,] "Pic 26 + 25"
[2,] "Pic 27 + 28"
[3,] "Pic 28 + 27"
[4,] "Pic 29 + 30"
[5,] "Pic 30 + 29"
[6,] "Pic 31 + 32"
Run Code Online (Sandbox Code Playgroud)
在这一点上,我获得了比我想要的更多的数字,因为它也抓住了其他唯一的数字(25).
我实际上尝试过使用gsub,但没有任何工作.帮助将非常感激!
我从另一个问题中拿了这个例子.我正在用Rcpp构建一个R包.我有一个像fun1(下面)这样的函数,我想把它放到自己的.cpp文件中.然后我想调用fun1其他函数(如下fun()所示).我想fun1在一个单独的文件中,因为我将从不同.cpp文件中的几个Rcpp函数调用它.是否有某些include语句和我需要做的事情才能使fun1函数在.cppwhere fun()位置可访问?谢谢.
library(inline)
library(Rcpp)
a = 1:10
cpp.fun = cxxfunction(signature(data1="numeric"),
plugin="Rcpp",
body="
int fun1( int a1)
{int b1 = a1;
b1 = b1*b1;
return(b1);
}
NumericVector fun_data = data1;
int n = data1.size();
for(i=0;i<n;i++){
fun_data[i] = fun1(fun_data[i]);
}
return(fun_data);
")
Run Code Online (Sandbox Code Playgroud)
所以对于我的代码,我将有两个.cpp文件:
#include <Rcpp.h>
using namespace Rcpp;
// I think I need something here to make fun1.cpp available?
// [[Rcpp::export]]
Rcpp::NumericVector fun(Rcpp::NumericVector …Run Code Online (Sandbox Code Playgroud) 当列的元素具有不同的字符串数时,我遇到了拆分列值的问题.我可以在plyr中做到这一点,例如:
library(plyr)
column <- c("jake", "jane jane","john john john")
df <- data.frame(1:3, name = column)
df$name <- as.character(df$name)
df2 <- ldply(strsplit(df$name, " "), rbind)
View(df2)
Run Code Online (Sandbox Code Playgroud)
因此,我们的数据框的列数与给定元素中的最大蜇数有关.
当我尝试在dplyr中执行此操作时,我使用了do函数:
library(dplyr)
df2 <- df %>%
do(data.frame(strsplit(.$name, " ")))
Run Code Online (Sandbox Code Playgroud)
但是我收到一个错误:
Run Code Online (Sandbox Code Playgroud)Error in data.frame("jake", c("jane", "jane"), c("john", "john", "john" : arguments imply differing number of rows: 1, 2, 3
在我看来应该使用rbind功能,但我不知道在哪里.
我想将data.table行转换为向量.这对我有用:
unlist(dt[row_num])
Run Code Online (Sandbox Code Playgroud)
但是有更原生的解决方案吗?我也不喜欢上面保留名称的时候我真的想要一个纯数字向量,然后导致:
as.numeric(unlist(dt[row_num]))
Run Code Online (Sandbox Code Playgroud)
似乎应该有更好的选择.
我有一个时间向量,它是因子的格式.例如,Time [1]是8:01:01,class(Time [1])是factor.
现在我想从这个向量中提取小时和分钟.这种计算效率最高的方法是什么?我的矢量非常大.非常感谢,
典型情况如下:
library(dplyr)
library(xgboost)
Run Code Online (Sandbox Code Playgroud)
当我导入库xgboost,函数slice的dplyr被掩盖的,我不得不写dplyr::slice,即使我从来没有使用xgboost::slice明确.
这个问题的明显的解决方案是导入xgboost之前dplyr.但是导入所有可能影响dplyr事先功能的库是很疯狂的.此外,当我使用caret库时,这个问题经常发生.即train函数自动导入所需的库,并且当时屏蔽了一些函数.
xgboost::slice使用早期导入的函数(例如dplyr::slice)屏蔽"屏蔽功能" (例如)?笔记
我有以下类型的数据帧:
Country <- rep(c("USA", "AUS", "GRC"),2)
Year <- 2001:2006
Level <- c("rich","middle","poor",rep(NA,3))
df <- data.frame(Country, Year,Level)
df
Country Year Level
1 USA 2001 rich
2 AUS 2002 middle
3 GRC 2003 poor
4 USA 2004 <NA>
5 AUS 2005 <NA>
6 GRC 2006 <NA>
Run Code Online (Sandbox Code Playgroud)
我想用右列中的最后一个用正确的级别标签填充缺失的值.
所以预期的结果应该是这样的:
Country Year Level
1 USA 2001 rich
2 AUS 2002 middle
3 GRC 2003 poor
4 USA 2004 rich
5 AUS 2005 middle
6 GRC 2006 poor
Run Code Online (Sandbox Code Playgroud) 我最近在"经济学人"中看到了一个折线图,其中标题的颜色与匹配折线图中使用的组的颜色相匹配.我想知道如何使用ggplot2对象执行此操作.下面是一些代码,用于制作包含所有内容的折线图,例如标题中除了彩色字之外的经济学文章.在底部,我显示了所需的输出.
这个问题不是关于显示此信息的理论方法(如直接标记或图例),而是专门用于着色标题中的单个单词.
data <- data.frame(
group = rep(c('affluence', 'poverty'), each = 6),
year = rep(c(1970, 1980, 1990, 2000, 2010, 2012), 2),
concentration = c(.125, .12, .14, .13, .145, .146, .068, .09, .125, .119, .13, .135)
)
library(ggplot2)
ggplot(data, aes(year, concentration, color = group)) +
geom_line(size = 1.5) +
geom_point(size = 4) +
scale_y_continuous(limits = c(0, .15)) +
labs(
x = NULL, y = NULL,
title = 'Concentration of affluence and poverty nationwide'
) +
theme_minimal() +
theme(
legend.position …Run Code Online (Sandbox Code Playgroud) 我在R中有两个矩阵,我想要乘法:
a = matrix(rnorm(20*10000, mean=0, sd=5), 20, 10000)
b = matrix(rnorm(20*10000, mean=0, sd=5), 20, 10000)
t(a)%*%b
Run Code Online (Sandbox Code Playgroud)
鉴于此矩阵乘法中较大的维数需要花费大量时间,是否有一种特定的方法可以使计算更快?R中是否有任何内置功能可以使这种乘法更快?
我需要得到这个:
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
[1,] 1 0 2 0 3 0 4 0 5
[2,] 0 0 0 0 0 0 0 0 0
[3,] 6 0 7 0 8 0 9 0 10
[4,] 0 0 0 0 0 0 0 0 0
[5,] 11 0 12 0 13 0 14 0 15
[6,] 0 0 0 0 0 0 0 0 0
[7,] 16 0 17 0 18 0 19 0 20 …Run Code Online (Sandbox Code Playgroud)