9

我有一些混合类型的数据,我想将它们存储在某种 R 数据结构中。每个数据点都有一组固定属性,可以是一维数字、因子或字符,还有一组可变长度数据。例如:

id  phrase                    num_tokens  token_lengths
1   "hello world"             2           5 5
2   "greetings"               1           9
3   "take me to your leader"  4           4 2 2 4 6

实际值并非都可以相互计算,但这就是数据的特点。我要做的操作包括基于布尔函数对数据进行子集化(例如类似nchar(data$phrase) > 10or lapply(data$token_lengths, length) > 2)。我还想按索引对可变长度部分中的值进行索引和平均值。这不起作用,但是喜欢:mean(data$token_lengths[1], na.rm=TRUE))

我发现我可以通过将“token_lengths”设置为数组来将其塞入 data.frame:

d <- data.frame(id=c(1,2,3), ..., token_lengths=as.array(list(c(5,5), 9, c(4,2,2,4,6)))

但这是最好的方法吗?

4

5 回答 5

4

I would just use the data in the "long" format.

E.g.

> d1 <- data.frame(id=1:3, num_words=c(2,1,4), phrase=c("hello world", "greetings", "take me to your leader"))
> d2 <- data.frame(id=c(rep(1,2), rep(2,1), rep(3,5)), token_length=c(5,5,9,4,2,2,4,6))
> d2$tokenid <- with(d2, ave(token_length, id, FUN=seq_along))
> d <- merge(d1,d2)
> subset(d, nchar(phrase) > 10)
  id num_words                 phrase token_length tokenid
1  1         2            hello world            5       1
2  1         2            hello world            5       2
4  3         4 take me to your leader            4       1
5  3         4 take me to your leader            2       2
6  3         4 take me to your leader            2       3
7  3         4 take me to your leader            4       4
8  3         4 take me to your leader            6       5
> with(d, tapply(token_length, id, mean))
  1   2   3 
5.0 9.0 3.6 

Once the data is in the long format, you can use sqldf or plyr to extract what you want from it.

于 2010-02-24T14:51:31.330 回答
4

试图将数据硬塞到数据框中对我来说似乎很骇人听闻。最好将每一行视为一个单独的对象,然后将数据集视为这些对象的数组。

此函数将您的数据字符串转换为适当的格式。(这是 S3 风格的代码;您可能更喜欢使用“适当的”面向对象系统之一。)

as.mydata <- function(x)
{
   UseMethod("as.mydata")
}

as.mydata.character <- function(x)
{
   convert <- function(x)
   {
      md <- list()
      md$phrase = x
      spl <- strsplit(x, " ")[[1]]
      md$num_words <- length(spl)
      md$token_lengths <- nchar(spl)
      class(md) <- "mydata"
      md
   }
   lapply(x, convert)
}

现在你的整个数据集看起来像

mydataset <- as.mydata(c("hello world", "greetings", "take me to your leader"))

mydataset
[[1]]
$phrase
[1] "hello world"

$num_words
[1] 2

$token_lengths
[1] 5 5

attr(,"class")
[1] "mydata"

[[2]]
$phrase
[1] "greetings"

$num_words
[1] 1

$token_lengths
[1] 9

attr(,"class")
[1] "mydata"

[[3]]
$phrase
[1] "take me to your leader"

$num_words
[1] 5

$token_lengths
[1] 4 2 2 4 6

attr(,"class")
[1] "mydata"

你可以定义一个打印方法来使它看起来更漂亮。

print.mydata <- function(x)
{
   cat(x$phrase, "consists of", x$num_words, "words, with", paste(x$token_lengths, collapse=", "), "letters.")
}
mydataset
[[1]]
hello world consists of 2 words, with 5, 5 letters.
[[2]]
greetings consists of 1 words, with 9 letters.
[[3]]
take me to your leader consists of 5 words, with 4, 2, 2, 4, 6 letters.

对于这种格式的数据,您想要执行的示例操作相当简单。

sapply(mydataset, function(x) nchar(x$phrase) > 10)
[1]  TRUE FALSE  TRUE
于 2010-02-24T12:01:59.020 回答
4

另一种选择是将您的数据框转换为模式列表矩阵 - 矩阵的每个元素都是一个列表。标准数组操作(使用[、apply() 等进行切片将适用)。

> d <- data.frame(id=c(1,2,3), num_tokens=c(2,1,4), token_lengths=as.array(list(c(5,5), 9, c(4,2,2,4,6))))
> m <- as.matrix(d)
> mode(m)
[1] "list"
> m[,"token_lengths"]
[[1]]
[1] 5 5

[[2]]
[1] 9

[[3]]
[1] 4 2 2 4 6

> m[3,]
$id
[1] 3

$num_tokens
[1] 4

$token_lengths
[1] 4 2 2 4 6
于 2010-02-24T18:08:13.580 回答
1

由于 R 数据帧结构松散地基于 SQL 表,因此数据帧的每个元素都不是原子数据类型是不常见的。但是,正如您所展示的,它可以完成,并且这个链接的帖子描述了这样一个在更大范围内实现的应用程序。

另一种方法是将数据存储为字符串并具有检索它的函数,或者创建一个单独的函数来附加数据并使用存储在数据框中的索引提取它。

> ## alternative 1
> tokens <- function(x,i=TRUE) Map(as.numeric,strsplit(x[i],","))
> d <- data.frame(id=c(1,2,3), token_lengths=c("5,5", "9", "4,2,2,4,6"))
> 
> tokens(d$token_lengths)
[[1]]
[1] 5 5

[[2]]
[1] 9

[[3]]
[1] 4 2 2 4 6

> tokens(d$token_lengths,2:3)
[[1]]
[1] 9

[[2]]
[1] 4 2 2 4 6

> 
> ## alternative 2
> retrieve <- local({
+   token_lengths <- list(c(5,5), 9, c(4,2,2,4,6))
+   function(i) token_lengths[i]
+ })
> 
> d <- data.frame(id=c(1,2,3), token_lengths=1:3)
> retrieve(d$token_lengths[2:3])
[[1]]
[1] 9

[[2]]
[1] 4 2 2 4 6
于 2010-02-24T02:38:08.047 回答
0

我也会对可变长度数据使用字符串,但如下例所示:“c(5,5)”表示第一个短语。需要用来eval(parse(text=...))进行计算。

例如,mean可以如下计算:

sapply(data$token_lengths,function(str) mean(eval(parse(text=str))))

于 2010-03-04T14:26:11.050 回答