如何在data.frame中动态创建新列?

时间:2015-06-14 13:31:11

标签: r

与问题类似

How to name variables on the fly?

基本上我想在移动中在data.frame中创建一个新列。可能是不好的做法,但是我正在做一个好的解决方案。

现在我已经尝试过:

Option Strict On
Public Class Form1
    Dim balance As Double
    Dim numDeposits As Long = 0
    Dim numChecks As Long = 0
    Dim numCharges As Long = 0
    Dim amountDeposits, amountChecks, amountCharges As Double
    Dim transAmount As Double
    Private Property program As String



    Private Sub CalculateBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalculateBtn.Click


        'Get transaction amount from the textbox
        transAmount = CDbl(AmountBox.Text)

        'When the check button is clicked check to see if the transaction amount is larger
        'than the balance if it is display a message box and apply a service charge of $10
        If CheckBtn.Checked And transAmount > balance Then
            balance = balance - 10
            MsgBox("Insufficient funds")

        End If

        'If the user inputs a transaction amount that is a negative number
        'display a message box
        If transAmount < 0 Then
            MsgBox("Enter a valid number")
        End If

        'Check to see if the transaction amount is numeric
        If IsNumeric(transAmount) Then
            CalculateBtn.Enabled = True
        End If

        'Decide what operation to use depending on what radio button is clicked
        If DepositBtn.Checked Then
            balance = balance + transAmount
        ElseIf CheckBtn.Checked Then
            balance = balance - transAmount
        ElseIf ServiceBtn.Checked Then
            balance = balance - transAmount
        End If

        'Print the balance
        BalanceLbl.Text = balance.ToString("C2")
    End Sub

    Private Sub ExitBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitBtn.Click
        'When the exit button is clicked close the program
        Me.Close()
    End Sub

    Private Sub AboutBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AboutBtn.Click
        'Display the 'About' information
        MessageBox.Show("Program Name: Checkbook" & Environment.NewLine & Environment.NewLine &
                        "Programmer: Stephanie Correa" & Environment.NewLine & Environment.NewLine &
                        "Description: Checkbook application to track transactions" & Environment.NewLine &
                        Environment.NewLine & "Version 2.30")
    End Sub
    Private Sub AmountBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AmountBox.TextChanged
        'If the transaction amount is a valid numeric value then enable the calculate button
        If IsNumeric(AmountBox.Text) Then
            CalculateBtn.Enabled = True
        Else
            CalculateBtn.Enabled = False
        End If
    End Sub

    Private Sub SummaryBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SummaryBtn.Click

        'Print summary of transactions
        MessageBox.Show("Number of Deposits: " & numDeposits & Environment.NewLine & "Number of Checks: " &
               numChecks & Environment.NewLine & "Number of Service Charges: " & numCharges & Environment.NewLine &
               "Amount from Deposits: " & amountDeposits)

    End Sub

    Private Sub DepositBtn_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DepositBtn.CheckedChanged
        'Keep track of amount of deposits and total amount of money deposited
        Dim sum As Long = 0

        sum = sum + 1
        numDeposits = sum

        amountDeposits += transAmount
    End Sub

    Private Sub CheckBtn_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBtn.CheckedChanged
        'Keep track of amount of checks and total amount of money from checks 
        Dim checkSum As Long = 0

        checkSum = checkSum + 1
        numChecks = checkSum

        amountChecks += transAmount
    End Sub

    Private Sub ServiceBtn_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ServiceBtn.CheckedChanged
        'Keep track of amount of service charges and amount of money from charges
        Dim ChargesSum As Long = 0
        ChargesSum = ChargesSum + 1
        numCharges = ChargesSum

        amountCharges = 0
        amountCharges += transAmount
    End Sub
End Class

该功能在不中断的情况下运行。但是data.frame测试不包含带有标题&#39; cheese&#39;和虹膜$ Petal.Width的值是人们想象的应该。

1 个答案:

答案 0 :(得分:5)

建议不要使用assign进行此类操作。但是,如果您需要尝试assign,可能的选项是

 create.new_var <- function(x){
  assign('test', `[[<-`(test, x, value=test$Petal.Width), envir=.GlobalEnv)
 }
 test <- create.new_var('cheese')
 head(test,3)
 #  Sepal.Length Sepal.Width Petal.Length Petal.Width Species cheese
 #1          5.1         3.5          1.4         0.2  setosa    0.2
 #2          4.9         3.0          1.4         0.2  setosa    0.2
 #3          4.7         3.2          1.3         0.2  setosa    0.2

您还可以在函数

中添加数据和替换列参数
create.new_var <- function(dat, x, x1){
  str1 <- deparse(substitute(dat))
  assign(str1, `[[<-`(dat, x, value=dat[,x1]), envir=.GlobalEnv)
}

test <- create.new_var(test, 'cheese', 'Petal.Width')

以下是不使用assignpaste的选项(某些已删除的帖子也使用了类似方法)。

 create.new_var2 <- function(dat, x, x1){ 
                       dat[,x] <- dat[,x1]
                       dat}

 create.new_var2(test, 'cheese', 'Petal.Width')