使用循环中的变量在R中调用数据

时间:2017-02-17 10:43:55

标签: r ggplot2

我有几个数据框,想要在循环中处理它们。所以我尝试使用包含框架名称的变量来调用框架。如何使用变量来处理数据框?或者通常将变量的内容作为表达式?

ggplot

我理解为什么这段代码不起作用,但我不知道如何以另一种方式执行此操作。与geom_line()相同的问题,我想在Plot1 <- ggplot() ToDo <- c(1, 5, 3) # specifies dataframes to plot counter <- length(ToDo) while (counter!=0) { NumberOfDataframe <- ToDo[counter] nextExperiment <- paste0("Experiment", NumberOfDataframe) Plot1 + geom_line(data = nextExperiment, aes(x = Position_mm, y = Force_N))) counter <- counter - 1 } 中调用nextExperiment,失败并显示错误

  

ggplot2不知道如何处理类字符的数据。

如何告诉它使用表达式指向的表达式?

conn = sqlite3.connect('viber_messages2')

cur = conn.cursor()
cur = cur.execute("""SELECT DISTINCT messages.conversation_id
                FROM messages
                INNER JOIN participants  ON messages.participant_id = participants._id
                WHERE messages.conversation_id IS NOT NULL;""")

query = ("""SELECT messages._id, messages.body, messages.conversation_id, messages.participant_id, participants_info.number, participants_info.contact_name
        FROM messages
        INNER JOIN
        participants ON messages.participant_id = participants._id
        INNER JOIN
        participants_info ON participants.participant_info_id = participants_info._id;""")

with open('messages.html', 'w') as h, open('test.txt', 'w') as t:
for convo in cur.fetchall():
    df = pd.read_sql_query(query, conn, params=convo)

    # HTML WRITE
    h.write(df.to_html())
    h.write('<br/>')

    # TXT WRITE
    t.write(df.to_string())
    t.write('\n\n')

cur.close()
conn.close()

(这是我的第一篇文章。)

2 个答案:

答案 0 :(得分:0)

我不确定您的具体问题。据我所知,你不打算做什么。您应该将data.frame放在另一个R对象中。然后迭代该对象,为每个实验做任何你想做的事情。

我建议在列表中组合dfs,然后使用lapply()

a = list(exp1 = data.frame(a = rnorm(100),b=rnorm(100)),
     exp2 = data.frame(a = rnorm(100),b=rnorm(100)),
     exp3 = data.frame(a = rnorm(100),b=rnorm(100)))

lapply(a,summary)
$exp1
       a                  b           
Min.   :-2.05828   Min.   :-1.91026  
 1st Qu.:-0.61256   1st Qu.:-0.80438  
 Median : 0.09998   Median :-0.09383  
 Mean   : 0.01377   Mean   :-0.07228  
 3rd Qu.: 0.72794   3rd Qu.: 0.50432  
 Max.   : 1.91998   Max.   : 2.08383  

$exp2
   a                 b          
 Min.   :-2.0764   Min.   :-2.3634  
 1st Qu.:-0.6953   1st Qu.:-0.5302  
 Median : 0.0952   Median : 0.1206  
 Mean   : 0.1227   Mean   : 0.1186  
 3rd Qu.: 0.8570   3rd Qu.: 0.6945  
 Max.   : 3.2124   Max.   : 2.4100  

$exp3
       a                  b           
 Min.   :-2.46588   Min.   :-3.43183  
 1st Qu.:-0.44548   1st Qu.:-0.75116  
 Median : 0.09448   Median : 0.04380  
 Mean   : 0.15859   Mean   :-0.02339  
 3rd Qu.: 0.84165   3rd Qu.: 0.69042  
 Max.   : 2.40169   Max.   : 2.13928 

答案 1 :(得分:0)

我使用了几个函数来解决我的问题,核心发现是get():

  

如何告诉它使用表达式指向的表达式?

get(x)正是如此,它调用了x所指向的内容。

paste0()仅在函数需要字符串时才有效,但您可以使用变量paste0("exp", i, ".csv")构建字符串。

assign(string_var, read.csv())在循环中从csv文件批量创建数据帧时也很有用。

# Batch import of range or chosen x.csv, x = 1,2,..
ToDo <- c(1:38)
for (i in 1:length(ToDo)) {
  nextFileNr <- ToDo[i]
  filePath <- paste0("Instron Daten/", nextFileNr, ".csv")
  if (ToDo[i] < 10) {  # produce exp01,.. for better sorting with ls()
    ExpName <- paste0("exp0", nextFileNr)
  } else {
      ExpName <- paste0("exp", nextFileNr)
  }
  # assign(obj_with_name_contained, be_filled_with_content_of_this)
  assign(ExpName, read.csv2(filePath, header = TRUE, skip = 53))
}

# Delete unwanted coloumns in all data frames using loop and get()
for(i in 1:length(ToDo)) {
  if (ToDo[i] < 10) {  # if exp1 make it exp01
    tempPattern <- paste0("exp0",i)
  } else {
    tempPattern <- paste0("exp",i)
  }
  set(get(tempPattern), j = 6L, value = NULL)  # delete col 6
}

# Tare measurement by shifting values with mean of first 1500 entries
for(i in 1:length(ToDo)) {
  if (ToDo[i] < 10) {
    tempPattern <- paste0("exp0",i)
  } else {
    tempPattern <- paste0("exp",i)
  }
  meanValue <- mean(get(tempPattern)[0:1500,"Kraft_N"])
  for(i in 1:nrow(get(tempPattern))) {
    set(get(tempPattern), j = "Kraft_N", i = i, value = (get(tempPattern)$Kraft_N[i] - meanValue))
  }
}

# Batch place all in a list
listFW <- mget(ls(pattern = "exp"))

关于ggplot:在循环中添加图层似乎不起作用。在调用ggplot之前必须组合数据。按建议使用bind_rows

makeExpStrings <- function(ToDo) { # creating name strings for calling the chosen data
i <- integer(0) # initializing
chosenExpStrings <- character(0)
for (i in 1:length(ToDo)) { # get data specified in ToDo and append
  nextFileNr <- ToDo[i]
  if (ToDo[i] < 10) {  # adapting to naming scheme of data
    ExpName <- paste0("exp0", nextFileNr)
  } else {
    ExpName <- paste0("exp", nextFileNr)
  }
  chosenExpStrings<-append(chosenExpStrings, ExpName)
}
return(chosenExpStrings)
}
chosenExpStrings <- makeExpStrings(ToDo)
# combine all coloumns with identifier for coloring
combi <- bind_rows(listFW[chosenExpStrings], .id="expNr")
ggplot() + 
  geom_path(data = combi, size = 0.4, aes_string(x = "Position_mm", y = "Kraft_N", colour = "expNr"))