我正在使用一个大矩阵(250x250x30 = 1,875,000个单元格),我想要一种方法为这个矩阵中的每个单元设置任意数量的标志,这种方式易于使用且空间效率合理.
我最初的计划是一个250x250x30列表数组,其中每个元素都是这样的:["FLAG1","FLAG8","FLAG12"].然后我将其改为仅存储整数:[1,8,12].这些整数由getter/setter函数在内部映射到原始标志字符串.这仅使用250mb,每点8个标志,这在内存方面很好.
我的问题是:我是否错过了构建此类数据的另一种明显方法?
谢谢大家的建议.我最后将一些建议合而为一,遗憾的是,我只能选择一个答案而不得不与其他人一起生活:
编辑:我在这里使用的初始代码(使用集合作为3d numpy数组的基本元素)使用了大量内存.这个新版本在填充时使用大约500mb randint(0,2**1000).
import numpy
FLAG1=2**0
FLAG2=2**1
FLAG3=2**2
FLAG4=2**3
(x,y,z) = (250,250,30)
array = numpy.zeros((x,y,z), dtype=object)
def setFlag(location,flag):
array[location] |= flag
def unsetFlag(location,flag):
array[location] &= ~flag
Run Code Online (Sandbox Code Playgroud) 我有一个字符串向量,看起来像:
str <- c("bills slashed for poor families today", "your calls are charged", "complaints dept awaiting refund")
Run Code Online (Sandbox Code Playgroud)
我希望得到所有以字母结尾的单词s并删除s.我试过了:
gsub("s$","",str)
Run Code Online (Sandbox Code Playgroud)
但它不起作用,因为它试图匹配以s而不是单词结束的字符串.我试图得到一个看起来像的输出:
[1] bill slashed for poor familie today
[2] your call are charged
[3] complaint dept awaiting refund
Run Code Online (Sandbox Code Playgroud)
有关如何做到这一点的任何指示?谢谢
请帮助R新手
我有一份英国邮政编码清单 - 事实上是300万次观察.使用R将它们绘制到地图上的最佳方法是什么?
谢谢
对R来说很新,并且已经开始使用tidytext包.
我正在尝试使用参数来输入unnest_tokens函数,这样我就可以进行多列分析.所以不要这样
library(janeaustenr)
library(tidytext)
library(dplyr)
library(stringr)
original_books <- austen_books() %>%
group_by(book) %>%
mutate(linenumber = row_number(),
chapter = cumsum(str_detect(text, regex("^chapter [\\divxlc]",
ignore_case = TRUE)))) %>%
ungroup()
original_books
tidy_books <- original_books %>%
unnest_tokens(word, text)
Run Code Online (Sandbox Code Playgroud)
最后一行代码是:
output<- 'word'
input<- 'text'
tidy_books <- original_books %>%
unnest_tokens(output, input)
Run Code Online (Sandbox Code Playgroud)
但是我得到了这个:
check_input(x)出错:输入必须是任意长度的字符向量或字符向量列表,每个字符向量的长度为1.
我试过as.character()没有太多运气.
关于这将如何工作的任何想法?
所以我一直在尝试使用left_join将新数据集的列放到我的主数据集(称为employee)上
我已经仔细检查了矢量名称和我没有的清洁,似乎没有任何效果.这是我的代码.非常感谢任何帮助.
job_codes <- read_csv("Quest_UMMS_JobCodes.csv")
job_codes <- job_codes %>%
clean_names() %>%
select(job_code, pos_desc = pos_des_desc)
job_codes$is_nurse <- str_detect(tolower(job_codes$pos_desc), "nurse")
employee <- employee %>%
left_join(job_codes, by = "job_code")
Run Code Online (Sandbox Code Playgroud)
我不断得到的错误:eval中的错误(替换(expr),envir,enclos):在rhs中找不到'job_code'列,无法加入
这是结果
names(job_code)
> names(job_codes)
[1] "job_code" "pos_desc" "is_nurse"
names(employee)
> names(employee)
[1] "REC_NUM" "ZIP" "STATE"
[4] "SEX" "EEO_CLASS" "BIRTH_YEAR"
[7] "EMP_STATUS" "PROCESS_LEVEL" "DEPARTMENT"
[10] "JOB_CODE" "UNION_CODE" "SUPERVISOR"
[13] "DATE_HIRED" "R_SHIFT" "SALARY_CLASS"
[16] "EXEMPT_EMP" "PAY_RATE" "ADJ_HIRE_DATE"
[19] "ANNIVERS_DATE" "TERM_DATE" "NBR_FTE"
[22] "PENSION_PLAN" "PAY_GRADE" "SCHEDULE"
[25] "OT_PLAN_CODE" "DECEASED" "POSITION"
[28] "WORK_SCHED" "SUPERVISOR_IND" "FTE_TOTAL"
[31] …Run Code Online (Sandbox Code Playgroud) 我有一Mark列中有特定值的数据框。我想n在标记发生之前和之后提取值(包括带有标记的行)。
我通过使用indices <- which(df$Mark == 1)where 1is 我正在寻找的值来找到我需要的值。现在我需要例如之前 5 行和之后 5 行的索引(以及带有标记的那一行,所以总共 11 行)。
我正在考虑通过循环indices来增加和减少它n,然后用附加的indices. 不过会很恶心。
有没有更快的方法来做到这一点?例如在 dplyr 中?Base R 答案也可以。
PS:我读过类似的问题,但它似乎不适合我的问题。
这是df:
df <- structure(list(CH1 = c(-0.02838132, -0.02642141, -0.02511601,
-0.02443906, -0.02414024, -0.02417388, -0.02451562, -0.02393946,
-0.02242496, -0.02104852, -0.0198534, -0.018965, -0.01853905,
-0.01837877, -0.01857743, -0.01847437, -0.0176798, -0.01672419,
-0.01594565, -0.01522826, -0.01485198, -0.01484227, -0.01507997,
-0.01556828, -0.01534458, -0.01473233, -0.01376753, -0.01251296,
-0.0116294, -0.01064516, -0.00966026, -0.00970934, -0.00969434,
-0.00921217, -0.00881855, -0.00832315, …Run Code Online (Sandbox Code Playgroud) 我正在编写一个API 包装器来使用httr包查询英国邮政编码,当我使用GET请求时一切正常.在使用POST请求时我有点迷失.
以下是API 的文档说明:
接受包含邮政编码数组的JSON对象.返回匹配的邮政编码和相应的可用数据列表.
最多可接受100个邮政编码.
POST https://api.postcodes.io/postcodes?q=[postcode]
发布数据
此方法需要包含要发布的邮政编码数组的JSON对象.例如
{"postcodes":["PR3 0SG","M45 6GN","EX165BL"]}
我尝试了以下方法:
library(httr)
pc_json <- '{
"postcodes" : ["PR3 0SG", "M45 6GN", "EX165BL"]
}'
r <- POST(paste0("https://api.postcodes.io/postcodes?q=", pc_json, encode = "json"))
Run Code Online (Sandbox Code Playgroud)
但它返回这个:
$ status 1 400
$ error 1 "提交的JSON无效.您需要提交带有一组邮政编码或地理位置对象的JSON对象"
修剪数组并使用它时会发生同样的情况:
r <- POST("https://api.postcodes.io/postcodes?q=EX165BL")
content(r)
Run Code Online (Sandbox Code Playgroud)
我在这里和这里读了类似的线程,但是他们没有让我的问题更容易解决.
任何想法如何解决它?
假设我有一个具有以下结构的数据集:
例如:
customer.id <- c(1,2,3,4,5,6,7,8)
product <- c("product1","product2","product1","product2","product1","product2","product1","product2")
country <- c("country1","country2","country1","country2","country1","country2","country1","country2")
payment.partner <- c("pp1","pp2","pp1","pp2","pp1","pp2","pp1","pp2")
day <- c("day1","day2","day1","day2","day1","day2","day1","day2")
price <- c("price1","price2","price1","price2","price1","price2","price1","price2")
customer.data <- data.frame(customer.id,product,country,payment.partner,day,price)
customer.data <- data.table(customer.data)
Run Code Online (Sandbox Code Playgroud)
假设我想从中生成聚合,例如,为每个组合执行预测算法.为此,我确定每个条件的唯一项,并按如下方式迭代:
unique.products <- droplevels(unique(customer.data[,product]))
unique.countries <- droplevels(unique(customer.data[,country]))
unique.payment.partners <- droplevels(unique(customer.data[,payment.partner]))
unique.days <- droplevels(unique(customer.data[,day]))
unique.prices <- droplevels(unique(customer.data[,price]))
for(i in seq_along(unique.products)){
temp.data1 <- customer.data[product==unique.products[[i]]]
for(j in seq_along(unique.countries)){
temp.data2 <- temp.data1[country==unique.countries[[j]]]
for(k in seq_along(unique.payment.partners)){
temp.data3 <- temp.data2[payment.partner==unique.payment.partners[[k]]]
for(l in seq_along(unique.days)){
temp.data4 <- temp.data3[day==unique.days[[l]]]
for(m in seq_along(unique.prices)){
temp.data5 <- temp.data4[price==unique.prices[[m]]]
if(nrow(temp.data5)!=0){
# do …Run Code Online (Sandbox Code Playgroud) 我有一个'数据'框架,有多列,其中一个是'Runtime',它有两种格式的数据:
Runtime
1 h 10 min
67 min
1 h 0 min
86 min
97 min
Run Code Online (Sandbox Code Playgroud)
我想将它们全部转换成分钟.尝试'strsplit'和'strip_split_fixed'.谁能告诉我一个实现目标,分裂或任何其他方法的方法?
先感谢您 !