我有一个数据集,该数据集的列数前面带有以下约定“测验。{1-33之间的数字}。{variable_name}”。例如,“ quiz.1.player.trial_counter”或“ quiz.33.submitted_answer”
我目前正在像这样抓取这些列:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
driver = webdriver.Firefox()
driver.get("http://zoeken.mol.bibliotheek.be/?itemid=|library/marc/vlacc|9394694&undup=false")
#waiting until the icon is loaded...
WebDriverWait(driver, 20).until(
EC.presence_of_element_located((By.XPATH, """//*[@id="availabilityStatic"]/div/div/ul/li/ul/li/span/i""")))
circle_icon = driver.find_element_by_xpath("""//*[@id="availabilityStatic"]/div/div/ul/li/ul/li/span/i""")
icon_class = circle_icon.get_attribute("class")
if "loanedout" in icon_class:
print "item not available"
else:
print "item available"
driver.quit()
每一行都是一个人的答案。我希望有一列从每个测验序言中提取数字,而另一列描述该变量。
假设我有这个简化的数据集:
dat[, c(names(data[, grep('quiz\\.[0-9]*.', names(dat))]))]
这是所需样本输出的一部分
dat <- read.table(text="ID quiz.1.player.question quiz.1.player.solution quiz.2.player.question quiz.2.player.solution
Trx1 A Yes 4 No
Trx1 B Maybe 5 No
Trx2 E No 6 Why
Trx2 B Yes 8 Yes
Trx3 B Definitely 9 Maybe
Trx3 C Yes 8 Yes
Trx5 F No 7 Why
Trx5 D Maybe 0 Ha", header=T)
我已经使用output <- read.table(text="id quiz_number key value
Trx1 1 player.question A
Trx1 1 player.solution Yes
Trx1 2 player.question 4
Trx1 2 player.solution No
Trx2 1 player.question B
", header=T)
和gather
和separate
尝试了各种方法,但是无法弄清楚如何实现所需的效果。
答案 0 :(得分:2)
对于tidyverse
解决方案,我们可以将数据从宽到长整形,然后再创建separate
条目以重现预期的输出格式。
library(tidyverse)
dat %>%
gather(k, value, -ID) %>%
separate(k, c("quiz_number", "key"), "\\.(?=player)") %>%
mutate(quiz_number = str_replace(quiz_number, "quiz\\.", ""))
# ID quiz_number key value
#1 Trx1 1 player.question A
#2 Trx1 1 player.question B
#3 Trx2 1 player.question E
#4 Trx2 1 player.question B
#5 Trx3 1 player.question B
#6 Trx3 1 player.question C
#7 Trx5 1 player.question F
#8 Trx5 1 player.question D
#9 Trx1 1 player.solution Yes
#10 Trx1 1 player.solution Maybe
#11 Trx2 1 player.solution No
#12 Trx2 1 player.solution Yes
#13 Trx3 1 player.solution Definitely
#14 Trx3 1 player.solution Yes
#15 Trx5 1 player.solution No
#16 Trx5 1 player.solution Maybe
#17 Trx1 2 player.question 4
#18 Trx1 2 player.question 5
#19 Trx2 2 player.question 6
#20 Trx2 2 player.question 8
#21 Trx3 2 player.question 9
#22 Trx3 2 player.question 8
#23 Trx5 2 player.question 7
#24 Trx5 2 player.question 0
#25 Trx1 2 player.solution No
#26 Trx1 2 player.solution No
#27 Trx2 2 player.solution Why
#28 Trx2 2 player.solution Yes
#29 Trx3 2 player.solution Maybe
#30 Trx3 2 player.solution Yes
#31 Trx5 2 player.solution Why
#32 Trx5 2 player.solution Ha