将数据从像data.frame这样的列表转换为像data.frame这样的矩阵?

时间:2017-05-21 19:22:49

标签: r dataframe

我想将数据从表单转换为像表格一样的矩阵。

我的数据如下所示:

dataFrame.list

     compound    time    concentration
1    compA        1       0.5
2    compA        2       0.3
3    compB        2       0.7
4    compB        3       0.8
5    compA        3       0.5
6    compC        1       0.4
7    compC        3       0.1

我想以下列形式得到它:

     compound    time1     time2     time3
1    compA        0.5       0.3       0.5
2    compB        NA        0.7       0.8
3    compC        0.4       NA        0.1

我遇到了几个问题:

dataframe.matrix

  1. 构建新的空数据框
  2. 使用变量名称寻址正确的列。
  3. 这是我到目前为止所做的:

    # uniqe list of compounds
    uniqueCompoundList <- unique(dataFrame.list$compound)
    
    
    # unique vector of times
    compoundTime <- as.character(sort(unique(dataFrame.list$time)))
    
    
    # new matrix like dataframe (This doesn't work but kind of shows what I would like)
    dataframe.matrix <- data.frame("compound", compoundTime)
    
    for (singleCompound in uniqueCompoundList){
    
       # subset based on name
       singeCompoundData <- subset(dataFrame.list, compound == singleCompound)
    
       # put name in new matrix
       dataframe.matrix$compound <- compound
    
       for (time in singleCompound$time)){
    
         # put the value of concentration under the column equal to time
         dataframe.matrix$time <- singleCompound$concentration
       }
    
    }
    

1 个答案:

答案 0 :(得分:1)

我们可以使用Radiobutton

from tkinter import *


root = Tk()

buttonA=IntVar()

R1 = Radiobutton(root, text="Button A", width = 30,  variable=buttonA, value=1)
R1.pack()

R2 = Radiobutton(root, text="Button B", width = 30, variable=buttonA, value=0)
R2.pack()


root.mainloop()

dcast

中的library(reshape2) dcast(df1, compound ~paste0("time", time)) # compound time1 time2 time3 #1 compA 0.5 0.3 0.5 #2 compB NA 0.7 0.8 #3 compC 0.4 NA 0.1
xtabs