从R中的向量元素中提取数据

时间:2014-10-20 21:19:56

标签: regex r csv vector

我正在尝试编写一个R脚本来解析来自csv文件单元格的有序数字对。 以下是CSV文件的前几行:

Test1,   Test2,                     Test3
Label1,  [(1, 2), (5, 6), (9, 10)], High
Label2,  [(5, 9), (6, 10)],         Low
Label3,  [(0, 5)],                  High

请注意,第二列是运行Python脚本产生的元组列表。我写了一个R脚本,使用read.csv将csv文件作为表读取,然后从每列创建向量。然后,我希望它从第2列的每个向量元素/单元格中读取每个有序对(元组),并将它们用于绘制矩形的开始和结束x值。但是我无法从向量元素中解析单个有序对(元组)。无论我做什么,R仍然将vector元素视为一个对象,而不是数组或列表。

这是R代码:

table1 <- read.csv("data.csv",header=TRUE,sep=",")
val1 <- paste(table1[,1])
val2 <- paste(table1[,2]) # First data row is [(1, 2), (5, 6), (9, 10)]
val3 <- paste(table1[,3])
nrows = length(val1)
for (i in 1:nrows) {
    rects <- val2[i]  # rects <- [(1, 2), (5, 6), (9, 10)]
    nval <- length(rects)  # Want nval to be 3
    if (nval > 0) {
        for (j in 1:nval) {
            bounds <- rects[j]  # Want bounds to be (1, 2), then (5, 6), then (9, 10)
            start <- bounds[1]  # Want start to be 1, 5, and then 9
            stop <- bounds[2]  # Want stop to be 2, 6, and then 10
            w <- stop - start # w should be 1
            vpp <- start + w/2 # vpp will be 1.5, 5.5, and then 9.5
            pushViewport(vp)
            grid.rect(x=0.5, y=0.5, width=w, height=0.5, gp=gpar(fill="violet"))
            upViewport()
         }
    }
}

1 个答案:

答案 0 :(得分:1)

我不确定我是否100%理解你想要的最终输出是什么,但这里有一种方法可以得到这样的数据帧,其中x和x的值分开:

   Test1                 Test3 X1 X2
1 Label1                  High  1  2
2 Label1                  High  5  6
3 Label1                  High  9 10
4 Label2                   Low  5  9
5 Label2                   Low  6 10
6 Label3                  High  0  5

我创建了您的数据框,但必须在粘贴的文本中手动替换分号。

df <- read.table(text = "Test1;   Test2;                     Test3
Label1;  [(1, 2), (5, 6), (9, 10)]; High
Label2;  [(5, 9), (6, 10)];         Low
Label3;  [(0, 5)];                 High", sep = ";", header = TRUE, stringsAsFactors = FALSE)

首先拆分"), ",这应该会分散每个点列表。然后将每个元组拆分为两列,并删除所有额外的括号等。

splits <- strsplit(as.character(df$Test2), "), ")

# split up list of tuples
df2 <- data.frame(Test1 = rep(df$Test1, lapply(splits, length)), 
           Test3 = rep(df$Test3, lapply(splits, length)),
           Test2 = unlist(splits), stringsAsFactors = FALSE)

# split tuples into two columns
df3 <- cbind(df2[, c("Test1", "Test3")], 
             data.frame(do.call("rbind", strsplit(df2$Test2, ",", fixed = TRUE))))

# remove parens etc. and convert to numeric
df3$X1 <- as.numeric(gsub("[^[:digit:]]", "", df3$X1))
df3$X2 <- as.numeric(gsub("[^[:digit:]]", "", df3$X2))

这将创建上面显示的数据框,并允许您绘制矩形,执行如下操作(将随机y值添加到数据框中):

library('dplyr')
library('ggplot2')
set.seed(10)
df4 <- df3 %>%
  do(mutate(., ymin = sample(10, nrow(.)))) %>% # random y values for plotting
  mutate(ymax = ymin + 1)

ggplot(df4, aes(xmin = X1, xmax = X2, ymin = ymin, ymax = ymax)) +
  geom_rect()

这看起来像: plot