for csv.gz的循环问题

时间:2019-02-20 20:13:08

标签: r for-loop dplyr readr

因此,我运行了这段代码来创建一个名为“ clickthrough”的变量。 if语句中的逻辑是正确的(如果行“ i”具有“ searchresultPage”的作用,而下一行具有“ visitPage”的作用,则在所有其他情况下,在名为“ clickthrough”的新列中为TRUE和FALSE)。但是,在我下载其他软件包以进行另一门课程之前,不确定是否会干扰我在这里需要的软件包。

数据

 hw3 <- structure(list(uuid = c("00000736167c507e8ec225bd9e71f9e5", 
 "00000c69fe345268935463abbfa5d5b3", 
 "00003bfdab715ee59077a3670331b787", "0000465cd7c35ad2bdeafec953e08c1a", 
 "000050cbb4ef5b42b16c4d2cf69e6358"), timestamp = c(20160301103842, 
 20160307005226, 20160302145305, 20160302222701, 20160302095023
 ), session_id = c("78245c2c3fba013a", "c559c3be98dca8a4", 
 "760bf89817ce4b08", 
 "fb905603d31b2071", "c2bf5e5172a892dc"), group = c("b", "a", 
 "a", "a", "a"), action = c("searchResultPage", "searchResultPage", 
 "checkin", "checkin", "checkin"), checkin = c(NA, NA, 30L, 60L, 
 30L), page_id = c("cbeb66d1bc1f1bc2", "eb658e8722aad674", "f99a9fc1f7fdd21e", 
 "e5626962a6939a75", "787dd6a4c371cbf9"), n_results = c(5L, 10L, 
 NA, NA, NA), result_position = c(NA, NA, NA, 10L, NA)), row.names = c(NA, 
 -5L), class = c("tbl_df", "tbl", "data.frame"), spec = structure(list(
     cols = list(uuid = structure(list(), class = c("collector_character", 
     "collector")), timestamp = structure(list(), class = 
 c("collector_double", 
     "collector")), session_id = structure(list(), class = 
 c("collector_character", 
     "collector")), group = structure(list(), class = 
 c("collector_character", 
     "collector")), action = structure(list(), class = 
 c("collector_character", 
     "collector")), checkin = structure(list(), class = c("collector_integer", 
     "collector")), page_id = structure(list(), class = 
 c("collector_character", 
     "collector")), n_results = structure(list(), class = c("collector_integer", 
     "collector")), result_position = structure(list(), class = 
 c("collector_integer", 
     "collector"))), default = structure(list(), class = 
 c("collector_guess", 
     "collector"))), class = "col_spec"))

我的代码:

  library(readr)
hw3 <- read_csv("C:/Users/johnt/Desktop/Statistical Data Mining/HW 0/events_log.csv(1).gz")


library(dplyr)

#We need to see if the user clicked immediately, let's run a loop
for(i in 1:nrow(hw3)){
  if(hw3$action[i]== "searchResultPage"){
    if(hw3$action[i+1]== "visitPage"){
    hw3$clickthrough[i] == "TRUE"
    }
    else{
    hw3$clickthrough[i] == "FALSE"
        }
  }
  else {
    hw3$clickthrough[i] == "FALSE"
       }
}

另外,我在第二天为另一个课程工作时运行了这段代码。

if(!requireNamespace("magrittr")) install.packages("magrittr")
if(!requireNamespace("tidyr")) install.packages("tidyr")
if(!requireNamespace("dplyr")) install.packages("dplyr")
if(!requireNamespace("ggplot2")) install.packages("ggplot2")
if(!requireNamespace("igraph")) install.packages("igraph")
if(!requireNamespace("ggraph")) install.packages("ggraph")

0 个答案:

没有答案