9

何らかの種類の R データ構造に格納したい混合型データがあります。各データ ポイントには、1 次元の数値、係数、または文字などの固定属性のセットと、可変長データのセットがあります。例えば:

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) > 10またはのようなもの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

別のオプションは、データ フレームをモード リストのマトリックスに変換することです。マトリックスの各要素はリストになります。標準の配列操作 ( 、 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 に答える
4

「長い」形式のデータを使用するだけです。

例えば

> 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 

データが長い形式になったら、sqldf または plyr を使用して必要なものを抽出できます。

于 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 に答える
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 に答える