Skip to content
Prev 174379 / 398506 Next

struggling with pairlists

mauede at alice.it wrote:
not sure if you need pairlists;  as far as i understand, you need a list
of two-element tuples where the elements in each tuple are of difefernt
types (character and numeric).

once you have the two original vectors, there are a number of
approaches; e.g., create a list of lists:

    # dummy data  
    variables = letters[1:5]
    values = 1:5

    output = mapply(
        function(variable, value)
            list(variable=variable, value=value),
        variables, values, SIMPLIFY=FALSE)
    # a list of lists, where each sublist contains a variable
    output$a
    # list of "a" and 1
    output$a$variable
    # "a"
    output[[1]]$value
    # 1

(note the fancy 'SIMPLIFY'). 

but you'd probably find it convenient to use a data frame:

    # dummy data  
    variables = letters[1:5]
    values = 1:5

    output = data.frame(variable=variables, value=values,
stringsAsFactors=FALSE)
    # a data frame with variables and values in separate columns

    output$variables
    # "a" "b" "c" "d" "e"

    output[1,]
    # one-row data frame with "a" and 1

    output[1,]$variable
    # "a"


vQ