我有一个带有因子列的大型数据框,我需要通过用分隔符分割因子名称来划分为三个因子列.这是我目前的方法,这是一个非常慢的大数据框架(有时数百万行):
data <- readRDS("data.rds")
data.df <- reshape2:::melt.array(data)
head(data.df)
## Time Location Class Replicate Population
##1 1 1 LIDE.1.S 1 0.03859605
##2 2 1 LIDE.1.S 1 0.03852957
##3 3 1 LIDE.1.S 1 0.03846853
##4 4 1 LIDE.1.S 1 0.03841260
##5 5 1 LIDE.1.S 1 0.03836147
##6 6 1 LIDE.1.S 1 0.03831485
Rprof("str.out")
cl <- which(names(data.df)=="Class")
Classes <- do.call(rbind, strsplit(as.character(data.df$Class), "\\."))
colnames(Classes) <- c("Species", "SizeClass", "Infected")
data.df <- cbind(data.df[,1:(cl-1)],Classes,data.df[(cl+1):(ncol(data.df))])
Rprof(NULL)
head(data.df)
## Time Location Species SizeClass Infected Replicate Population
##1 1 1 …Run Code Online (Sandbox Code Playgroud) 想text在句子中分割出一个字符元素的向量.分裂标准有多种模式("and/ERT","/$").也有例外(:/$.,and/ERT then,./$. Smiley)自该模式.
尝试:匹配拆分应该的情况."^&*"在该位置插入一个不寻常的图案().strsplit具体模式
问题:我不知道如何正确处理异常.有明确的情况"^&*"应该消除异常模式()并在运行之前恢复原始文本strsplit.
码:
text <- c("This are faulty propositions one and/ERT two ,/$, which I want to split ./$. There are cases where I explicitly want and/ERT some where I don't want to split ./$. For example :/$. when there is an and/ERT then I don't want to split ./$. This is also one case where I …Run Code Online (Sandbox Code Playgroud) 我的变量名称格式如下:
PP_Sample_12.GT
Run Code Online (Sandbox Code Playgroud)
或者
PP_Sample-17.GT
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用字符串拆分来 grep 出中间部分: ieSample_12或Sample-17. 但是,当我这样做时:
IDtmp <- sapply(strsplit(names(df[c(1:13)]),'_'),function(x) x[2])
IDs <- data.frame(sapply(strsplit(IDtmp,'.GT',fixed=T),function(x) x[1]))
Run Code Online (Sandbox Code Playgroud)
我最终得到的Sample是PP_Sample_12.GT。
还有其他方法可以做到这一点吗?也许使用模式/替换类型的函数?不过,不确定 R 中是否存在(但我认为这可能适用gsub)
从URL的字符向量开始.我们的目标是与公司的唯一名称最终,只意味着一列"test","example"并"sample"在下面的例子.
urls <- c("http://grand.test.com/", "https://example.com/",
"http://.big.time.sample.com/")
Run Code Online (Sandbox Code Playgroud)
删除".com"可能跟随它的任何内容并保留第一部分:
urls <- sapply(strsplit(urls, split="(?<=.)(?=\\.com)", perl=T), "[", 1)
urls
# [1] "http://grand.test" "https://example" "http://.big.time.sample"
Run Code Online (Sandbox Code Playgroud)
我的下一步是使用链式调用删除http://和https://部分gsub():
urls <- gsub("^http://", "", gsub("^https://", "", urls))
urls
# [1] "grand.test" "example" ".big.time.sample"
Run Code Online (Sandbox Code Playgroud)
但这是我需要帮助的地方.如何在第一个和第三个网址字符串中处理公司名称之前的多个句点(点)?例如,下面的调用返回第二个字符串的NA,因为该"example"字符串没有剩余句点.或者,如果我只保留第一部分,我会丢失公司名称.
urls <- sapply(strsplit(urls, split = "\\."), "[", 2)
urls
# [1] "test" NA "big"
urls <- sapply(strsplit(urls, split = "\\."), "[", 1)
urls
# [1] "grand" "example" ""
Run Code Online (Sandbox Code Playgroud)
也许是一个 …
我有一个婴儿名字的文件,我正在阅读,然后试图得到婴儿名字中的最后一个字符.例如,该文件看起来像..
Name Sex
Anna F
Michael M
David M
Sarah F
Run Code Online (Sandbox Code Playgroud)
我在使用中读到了这个
sourcenames = read.csv("babynames.txt", header=F, sep=",")
Run Code Online (Sandbox Code Playgroud)
我最终希望结果看起来像......
Name Last Initial Sex
Michael l M
Sarah h F
Run Code Online (Sandbox Code Playgroud)
我已设法将名称拆分为单独的字符..
sourceout = strsplit(as.character(sourcenames$Name),'')
Run Code Online (Sandbox Code Playgroud)
但是现在我被困在哪里是如何得到最后一封信,所以在迈克尔的情况下,如何得到'我'.我认为tail()可能会工作,但它会返回最后几条记录,而不是每个Name元素中的最后一个字符.
非常感谢任何帮助或建议.
谢谢 :)
我想缩短data.frame的一列值.现在,每个值都包含许多字母,例如
df$col1
[1] AHG ALK OPH BCZ LKH QRQ AAA VYY
Run Code Online (Sandbox Code Playgroud)
我需要的只是第一封信:
df$col1
[1] A A O B L Q A V
Run Code Online (Sandbox Code Playgroud)
我已阅读,建议使用其他条目gsub,stri_replace_all_charclass或strsplit.但我担心我需要帮助来实现这一点.
我有一个如下所示的数据框:
V1 V2
peanut butter sandwich 2 slices of bread 1 tablespoon peanut butter
Run Code Online (Sandbox Code Playgroud)
我的目标是:
V1 V2
peanut butter sandwich 2 slices of bread
peanut butter sandwich 1 tablespoon peanut butter
Run Code Online (Sandbox Code Playgroud)
我试图分裂字符串使用strsplit(df$v2, " "),但我只能拆分" ".我不确定你是否只能在第一个数字处拆分字符串,然后取字符直到下一个数字.
我有这个df:
dput(df)
structure(list(URLs = c("http://bursesvp.ro//portal/user/_/Banco_Votorantim_Cartoes/0-7f2f5cb67f1-22918b.html",
"http://46.165.216.78/.CartoesVotorantim/Usuarios/Cadastro/BV6102891782/",
"http://www.chalcedonyhotel.com/images/promoc/premiado.tam.fidelidade/",
"http://bmbt.ro/portal/a3/_Votorantim_/VotorantimCartoes2016/0-7f2f5cb67f1-22928b.html",
"http://voeazul.nl/azul/")), .Names = "URLs", row.names = c(NA,
-5L), class = "data.frame")
Run Code Online (Sandbox Code Playgroud)
它描述了不同的URL,我试图计算主机名的字符数,无论是实际名称(http://hostname.com/...)还是IP(http://000.000.000.000)/..).但是,如果它是一个实际名称,那么我只想要www之间的nchar.和.com.如果它是IP,那么它的所有数字和"中间"点.
上述样本数据的预期结果:
exp_outcome
1 8
2 13
3 15
4 4
5 7
Run Code Online (Sandbox Code Playgroud)
我试图做一些strsplit但无法到达的地方.
如何使用 %、- 和 + 作为可能的分隔符将字符列拆分为 3 列,并将分隔符保留在新列中?
示例数据:
data <- data.table(x=c("92.1%+100-200","90.4%-1000+200", "92.8%-200+100", "99.2%-500-200","90.1%+500-200"))
Run Code Online (Sandbox Code Playgroud)
所需数据示例:
data.desired <- data.table(x1=c("92.1%", "90.4%", "92.8%","99.2%","90.1%")
, x2=c("+100","-1000","-200","-500","+500")
, x3=c("-200","+200","+100","-200","-200"))
Run Code Online (Sandbox Code Playgroud)
很高兴为一个好的答案和对这个问题的一些帮助奖励积分!
I have a dataframe df:
userID Score Task_Alpha Task_Beta Task_Charlie Task_Delta
3108 -8.00 Easy Easy Easy Easy
3207 3.00 Hard Easy Match Match
3350 5.78 Hard Easy Hard Hard
3961 10.00 Easy NA Hard Hard
4021 10.00 Easy Easy NA Hard
1. userID is factor variable
2. Score is numeric
3. All the 'Task_' features are factor variables with possible values 'Hard', 'Easy', 'Match' or NA
Run Code Online (Sandbox Code Playgroud)
I want to create new columns per userID that contain the counts of …
r ×10
strsplit ×10
regex ×5
string ×3
count ×1
data.table ×1
gsub ×1
mutate ×1
parsing ×1
performance ×1
reshape2 ×1
split ×1
stringr ×1
substring ×1
text-mining ×1