按列复制行,在R中将值更改为1或0

时间:2018-02-28 18:00:56

标签: r

我的数据结构如下:

A B C D 

3 2 1 1 

我希望将其重组为

A B C D

1 0 0 0

1 0 0 0

1 0 0 0

0 1 0 0

0 1 0 0

0 0 1 0

0 0 0 1

有关如何在R中执行此操作的任何想法?非常感谢。

2 个答案:

答案 0 :(得分:2)

如果输入是data.frame,您可以执行以下操作:

Imports System
Imports System.IO
Imports System.Net
Imports System.Text
Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        ' Create a request using a URL that can receive a post.   
        Dim request As WebRequest = WebRequest.Create("http://sms.orreryhim.com/api/postsms.php")
        ' Set the Method property of the request to POST.  
        request.Method = "POST"
        ' Create POST data and convert it to a byte array.  
        Dim postData As String = "<MESSAGE>
    <AUTHKEY>your authkey here</AUTHKEY>
    <ROUTE>route code</ROUTE>
    <CAMPAIGN>campaign mode</CAMPAIGN>
    <COUNTRY>country code</COUNTRY>
    <SENDER>sender id</SENDER>
    <SMS TEXT='"messges'">
        <ADDRESS TO="'mobile number'"></ADDRESS>
    </SMS>
</MESSAGE>"
        Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
        ' Set the ContentType property of the WebRequest.  
        request.ContentType = "application/xml"
        ' Set the ContentLength property of the WebRequest.  
        request.ContentLength = byteArray.Length
        ' Get the request stream.  
        Dim dataStream As Stream = request.GetRequestStream()
        ' Write the data to the request stream.  
        dataStream.Write(byteArray, 0, byteArray.Length)
        ' Close the Stream object.  
        dataStream.Close()
        ' Get the response.  
        Dim response As WebResponse = request.GetResponse()
        ' Display the status.  
        'Console.WriteLine(CType(response, HttpWebResponse).StatusDescription)
        Label1.Text = CType(response, HttpWebResponse).StatusDescription
        ' Get the stream containing content returned by the server.  
        dataStream = response.GetResponseStream()
        ' Open the stream using a StreamReader for easy access.  
        Dim reader As New StreamReader(dataStream)
        ' Read the content.  
        Dim responseFromServer As String = reader.ReadToEnd()
        ' Display the content.  
        Console.WriteLine(responseFromServer)
        Label2.Text = responseFromServer
        ' Clean up the streams.  
        reader.Close()
        dataStream.Close()
        response.Close()


    End Sub

End Class

这将产生如下矩阵:

coln <- seq_along(df)
m = do.call(rbind, lapply(coln, function(i) {t(replicate(df[1,i], coln == i))})) +0

然后,您可以将其转换为data.frame或设置列名称。

答案 1 :(得分:0)

以下是使用dcast

的选项
library(data.table)
nm1 <- rep(names(df1), unlist(df1))
dcast(data.table(nm1, v1 = seq_along(nm1)), v1 ~ nm1, length)[, v1 := NULL][]
#   A B C D
#1: 1 0 0 0
#2: 1 0 0 0
#3: 1 0 0 0
#4: 0 1 0 0
#5: 0 1 0 0
#6: 0 0 1 0
#7: 0 0 0 1

创建“nm1”后,请使用model.matrix

中的base R
model.matrix(~-1 + nm1)

或单行

model.matrix(~ -1 + rep(names(df1), unlist(df1)))

并更改列名

数据

df1 <- data.frame(A = 3, B = 2, C = 1, D = 1)