我目前正在处理日志文件中的数据,如下所示:
Subject: MS1989, Characters: G3, Position: TL, Block: 2, Reaction Time: 957 ms
Subject: MS1989, Characters: 7A, Position: TR, Block: 2, Reaction Time: 220 ms
如何对此进行转换,使其看起来更像这样:
Subject Characters Position Block Reaction Time
MS1989 G3 ...
之前可能已经回答了,但我真的不知道怎么说这个,所以搜索很难。也许重塑可能在这里很有用,但这似乎不是长格式或宽格式。
答案 0 :(得分:0)
我确信可以有更好的解决方案,
library(tidyr)
df = data.frame(x = c("Subject: MS1989, Characters: G3, Position: TL, Block: 2, Reaction Time: 957 ms",
"Subject: MS1989, Characters: 7A, Position: TR, Block: 2, Reaction Time: 220 ms"))
df$y <- gsub("Reaction Time:", "",
gsub("Block:", "",
gsub("Position:", "",
gsub("Characters:", "",
gsub("Subject:" ,"",df$x)))))
df$x <- NULL
df <- df %>% separate(y, into = c("Subject", "Character", "Position", "Block", "Reaction"),
sep = "[,]")
df
Subject Character Position Block Reaction
1 MS1989 G3 TL 2 957 ms
2 MS1989 7A TR 2 220 ms
答案 1 :(得分:0)
将数据复制到文本文件&#34; tidy.txt&#34;:
library(tidyverse)
untidy <- read_csv("untidy.txt", col_names=FALSE)
tidier <- untidy %>%
separate_rows(col=1:ncol(untidy), sep=":") %>%
distinct()
colnames(tidier) <- as.character(tidier[1,])
tidy <- tidier[-1,]
然后:
$input = '[image]http://picture.com/1.jpg[/image]';
$output_pf = '<img src="%s">';
if(preg_match('#\[image\](.+)\[/image\]#', $input, $matches)) {
$output = sprintf($output_pf, $matches[1]);
}
print $output;