如何在R中创建项目频率矩阵

时间:2017-02-03 13:42:00

标签: r matrix reshape dcast

我正在尝试使用以下数据在R中构建项目频率矩阵。

 <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_marginEnd="40dp"
    android:layout_marginTop="6dp"
    android:layout_marginBottom="6dp"
    android:layout_height="wrap_content"
    android:id="@+id/rl_msg"
    android:layout_gravity="center">

        <TextView
            android:paddingStart="8dp"
            android:text="Sender Name"
            android:maxLines="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tv_senderName"
            android:layout_alignParentTop="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentEnd="true" />

        <android.support.v7.widget.CardView
            android:layout_width="wrap_content"
            card_view:cardBackgroundColor="@color/cBgrMsg"
            card_view:cardElevation="6dp"
            android:layout_height="wrap_content"
            android:id="@+id/card_msgText"
            android:layout_below="@id/tv_senderName"
            card_view:cardCornerRadius="6dp">

        <TextView
        android:text="The message will come here which is gonna be a lot larger than this textView can handle and at that time to avoid any bugs or not to discard and functionalities I would have to utilize my mind a lot which is gonna be so tiresome and annoying that I  would think about leaving this app in middle without finishing it. Damn."
        android:padding="6dp"
        android:textSize="16sp"
        android:maxLength="500"
        android:layout_width="wrap_content"
        android:textColor="@color/cWhite"
        android:layout_height="wrap_content"
        android:id="@+id/tv_msg"
        android:layout_below="@id/tv_senderName" />

        </android.support.v7.widget.CardView>

    <TextView
        android:text="Nothing to show."
        android:padding="4dp"
        android:background="#0000"
        android:textSize="10sp"
        android:textStyle="italic"
        android:layout_width="wrap_content"
        android:gravity="start"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true"
        android:textColor="#999999"
        android:layout_height="wrap_content"
        android:id="@+id/tv_sentAt"
        android:layout_below="@id/card_msgText" />
</RelativeLayout>

我想要的只是一个表,其中唯一用户为行,audioID为列。我知道我需要使用dcast方法来做到这一点,但无法弄清楚到底该怎么做。我尝试了以下代码,但我知道它不是正确的解决方案。

H_USERID    H_AUDIOID
1   TRAAAAW128F429D538.h5
2   TRAAABD128F429CF47.h5
2   TRAAADZ128F9348C2E.h5
3   TRAAAEF128F4273421.h5
3   TRAAAFD128F92F423A.h5
3   TRAAAMO128F1481E7F.h5
3   TRAAAMQ128F1460CD3.h5
3   TRAAAPK128E0786D96.h5
3   TRAAARJ128F9320760.h5
4   TRAAAVG12903CFA543.h5
4   TRAAAVO128F93133D4.h5
4   TRAABCL128F4286650.h5
4   TRAABDL12903CAABBA.h5

所需的输出是:

user = read.csv("temp.csv")
step1 = melt(user)
output <- dcast(step1, user$H_AUDIOID~value, fun.aggregate = length)
output = t(output)

任何帮助都会有用。

0 个答案:

没有答案