An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20090320/f6e16ff0/attachment-0002.pl>
struggling with pairlists
3 messages · mauede at alice.it, jim holtman, Wacek Kusnierczyk
It was not clear what you were asking for. Did you want a 'list' of 'lists' pairs? What are you going to use the data for? How are you expecting to access it? This will create a list similar to: z <- list(list(variable="TrendOff", value=0), list(.......
flags_val <- c( 0,1,1,1,4,1,1,1,4,1,1,1,4,1,1,1,4,1,1)
flags_nam <- c("TrendOff","MOdwt","ZeroPadding",
+ "Step1HSOff","Step1NumHSOff","Step1NumHSOff","Step1ExtOff", + "Step2HSOff","Step2NumHSOff","Step2NumHSOff","Step2ExtOff", + "Step3HSOff","Step3NumHSOff","Step3NumHSOff","Step3ExtOff", + "Step4HSOff","Step4NumHSOff","Step4NumHSOff","Step4ExtOff" )
x <- lapply(seq_along(flags_val), function(z){
+ list(variable=flags_nam[z], value=flags_val[z]) + })
dput(x)
list(structure(list(variable = "TrendOff", value = 0), .Names = c("variable",
"value")), structure(list(variable = "MOdwt", value = 1), .Names =
c("variable",
"value")), structure(list(variable = "ZeroPadding", value = 1), .Names
= c("variable",
"value")), structure(list(variable = "Step1HSOff", value = 1), .Names
= c("variable",
"value")), structure(list(variable = "Step1NumHSOff", value = 4),
.Names = c("variable",
"value")), structure(list(variable = "Step1NumHSOff", value = 1),
.Names = c("variable",
"value")), structure(list(variable = "Step1ExtOff", value = 1), .Names
= c("variable",
"value")), structure(list(variable = "Step2HSOff", value = 1), .Names
= c("variable",
"value")), structure(list(variable = "Step2NumHSOff", value = 4),
.Names = c("variable",
"value")), structure(list(variable = "Step2NumHSOff", value = 1),
.Names = c("variable",
"value")), structure(list(variable = "Step2ExtOff", value = 1), .Names
= c("variable",
"value")), structure(list(variable = "Step3HSOff", value = 1), .Names
= c("variable",
"value")), structure(list(variable = "Step3NumHSOff", value = 4),
.Names = c("variable",
"value")), structure(list(variable = "Step3NumHSOff", value = 1),
.Names = c("variable",
"value")), structure(list(variable = "Step3ExtOff", value = 1), .Names
= c("variable",
"value")), structure(list(variable = "Step4HSOff", value = 1), .Names
= c("variable",
"value")), structure(list(variable = "Step4NumHSOff", value = 4),
.Names = c("variable",
"value")), structure(list(variable = "Step4NumHSOff", value = 1),
.Names = c("variable",
"value")), structure(list(variable = "Step4ExtOff", value = 1), .Names
= c("variable",
"value")))
On Fri, Mar 20, 2009 at 12:10 PM, <mauede at alice.it> wrote:
I would like to create a vector of pairlist (flag, binary_value) ?like: ?(variable ="TrendOff", ? ? value = 0) ?(variable ="MOdwt", ? ? ? ?value = 1) ?(variable ? "ZeroPadding", value =1) ?................................................ I tried the following syntax but the emcompassing list (that I called "flags") is not made up of pairlists. Instead it is made up of two apparently disjoint lists:
flags_val <- c( 0,1,1,1,4,1,1,1,4,1,1,1,4,1,1,1,4,1,1)
?flags_nam <- c("TrendOff","MOdwt","ZeroPadding",
+ ? ? ? ? ? ? ? ? "Step1HSOff","Step1NumHSOff","Step1NumHSOff","Step1ExtOff", + ? ? ? ? ? ? ? ? "Step2HSOff","Step2NumHSOff","Step2NumHSOff","Step2ExtOff", + ? ? ? ? ? ? ? ? "Step3HSOff","Step3NumHSOff","Step3NumHSOff","Step3ExtOff", + ? ? ? ? ? ? ? ? "Step4HSOff","Step4NumHSOff","Step4NumHSOff","Step4ExtOff" )
?flags_nam
?[1] "TrendOff" ? ? ?"MOdwt" ? ? ? ? "ZeroPadding" ? "Step1HSOff" ? ?"Step1NumHSOff" "Step1NumHSOff" ?[7] "Step1ExtOff" ? "Step2HSOff" ? ?"Step2NumHSOff" "Step2NumHSOff" "Step2ExtOff" ? "Step3HSOff" [13] "Step3NumHSOff" "Step3NumHSOff" "Step3ExtOff" ? "Step4HSOff" ? ?"Step4NumHSOff" "Step4NumHSOff" [19] "Step4ExtOff"
flags <- pairlist (data = flags_val, variable = flags_nam) flags
$data ?[1] 0 1 1 1 4 1 1 1 4 1 1 1 4 1 1 1 4 1 1 $variable ?[1] "TrendOff" ? ? ?"MOdwt" ? ? ? ? "ZeroPadding" ? "Step1HSOff" ? ?"Step1NumHSOff" "Step1NumHSOff" ?[7] "Step1ExtOff" ? "Step2HSOff" ? ?"Step2NumHSOff" "Step2NumHSOff" "Step2ExtOff" ? "Step3HSOff" [13] "Step3NumHSOff" "Step3NumHSOff" "Step3ExtOff" ? "Step4HSOff" ? ?"Step4NumHSOff" "Step4NumHSOff" [19] "Step4ExtOff" Please, can someone teach me how to create a vector of pairlists ? I could find no illustrating on-line example. Thank you very much. Maura tutti i telefonini TIM! ? ? ? ?[[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
mauede at alice.it wrote:
I would like to create a vector of pairlist (flag, binary_value) like: (variable ="TrendOff", value = 0) (variable ="MOdwt", value = 1) (variable ? "ZeroPadding", value =1) ................................................ I tried the following syntax but the emcompassing list (that I called "flags") is not made up of pairlists. Instead it is made up of two apparently disjoint lists:
flags_val <- c( 0,1,1,1,4,1,1,1,4,1,1,1,4,1,1,1,4,1,1)
flags_nam <- c("TrendOff","MOdwt","ZeroPadding",
+ "Step1HSOff","Step1NumHSOff","Step1NumHSOff","Step1ExtOff", + "Step2HSOff","Step2NumHSOff","Step2NumHSOff","Step2ExtOff", + "Step3HSOff","Step3NumHSOff","Step3NumHSOff","Step3ExtOff", + "Step4HSOff","Step4NumHSOff","Step4NumHSOff","Step4ExtOff" )
flags_nam
[1] "TrendOff" "MOdwt" "ZeroPadding" "Step1HSOff" "Step1NumHSOff" "Step1NumHSOff" [7] "Step1ExtOff" "Step2HSOff" "Step2NumHSOff" "Step2NumHSOff" "Step2ExtOff" "Step3HSOff" [13] "Step3NumHSOff" "Step3NumHSOff" "Step3ExtOff" "Step4HSOff" "Step4NumHSOff" "Step4NumHSOff" [19] "Step4ExtOff"
flags <- pairlist (data = flags_val, variable = flags_nam)
flags
$data [1] 0 1 1 1 4 1 1 1 4 1 1 1 4 1 1 1 4 1 1 $variable [1] "TrendOff" "MOdwt" "ZeroPadding" "Step1HSOff" "Step1NumHSOff" "Step1NumHSOff" [7] "Step1ExtOff" "Step2HSOff" "Step2NumHSOff" "Step2NumHSOff" "Step2ExtOff" "Step3HSOff" [13] "Step3NumHSOff" "Step3NumHSOff" "Step3ExtOff" "Step4HSOff" "Step4NumHSOff" "Step4NumHSOff" [19] "Step4ExtOff" Please, can someone teach me how to create a vector of pairlists ? I could find no illustrating on-line example. Thank you very much.
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