Re: [R] Missing index in vector assignment
Petr - your suggestion WORKS! Thank you so much, really! happy-Chaehan On Wed, Mar 17, 2010 at 2:22 PM, Petr PIKAL wrote: > Hi > > > r-help-boun...@r-project.org napsal dne 17.03.2010 13:04:05: > > > Jim & Petr, > > Thank you for your hint - I am really grateful, because they helped me > to > > get one step further, > > and although now the problem lies somewhere else, you encouraged that we > can > > find the > > solution soon! > > > > 1. To Petr's comments > > > > Petr, your hint to define y: > > y <- LVvar[,1, drop=FALSE] > > did solve the problem, so I got a data.frame with the indexes. > > Going through help page > > y <- as.matrix(LVvar[,1, drop=FALSE]) > x <- as.matrix(LVvar[,-1] > svp <- ksvm(x, y, type="nu-svc") > > shall work. However as I never used this package and function I am not > sure if it is used correctly. > > Regards > Petr > > > > > > Yet, then I turned to the call > > svp <- ksvm(x, y, type="nu-svc") > > Error in .local(x, ...) : y must be a vector or a factor. > > > > So then I followed your second advice, looking up the additional > information > > from help file: > > == > > x is defined as: > > a symbolic description of the model to be fit. When not using a formula > x > > can be a matrix or vector containing the training data or a kernel > matrix of > > class kernelMatrix of the training data or a list of character vectors > (for > > use with the string kernel). Note, that the intercept is always > excluded, > > whether given in the formula or not. > > > > y is defined as > > a response vector with one label for each row/component of x. Can be > either > > a factor (for classification tasks) or a numeric vector (for > regression). > > == > > > > So I tried to convert LVvar into a matrix via as.matrix() but didn't > make a > > difference. > > > > > > 2. To Jim's comments > > > > On Wed, Mar 17, 2010 at 1:10 AM, jim holtman wrote: > > > > > Please provide what LVvar is. > > > > > > > LVvar is a dataframe > > > > > > > At least provide str(LVvar), or preferably a 'dput' of the object. > > > > > > > str(LVvar) returns: > > > > 'data.frame': 55 obs. of 7 variables: > > $ rPerform : num 0.0682 -0.0682 -0.7443 0.7443 0.2619 ... > > $ rCoordCap: num 4.98 6.08 5.73 5.92 4.96 ... > > $ rKnowGrow: num 4.5 5.92 5.23 6.08 4.38 ... > > $ rGoalcom : num 5.81 6.58 6 5.75 5.29 ... > > $ rSupport : num 6.15 6.92 6.6 4.92 6 ... > > $ rOpcomm : num 5.98 6.25 6.33 6.5 5.29 ... > > $ rT2Cadap : num 5.03 6.12 4.9 6.25 5.12 ... > > - attr(*, "na.action")=Class 'omit' Named int 40 > > .. ..- attr(*, "names")= chr "40" > > > > == > > > > dput(LVvar) returns (abbreviated with ...): > > > > structure(list(rPerform = c(0.0681818181818183, -0.0681818181818183, > > -0.744318181818182, 0.744318181818182, 0.261931818181818, > > -0.900568181818182, > > ... > > rCoordCap = c(4.979167, > > 6.08, 5.73, 5.916667, 4.958333, > > ... > > .Names = c("rPerform", "rCoordCap", "rKnowGrow", > > "rGoalcom", "rSupport", "rOpcomm", "rT2Cadap"), row.names = c(1L, > > 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, > > 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, > > 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 41L, 42L, > > 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L, 55L, > > 56L), na.action = structure(40L, .Names = "40", class = "omit"), class = > > "data.frame") > > > > > > > > > > > > PLEASE do read the posting guide > > > http://www.R-project.org/posting-guide.html<http://www.r-project.org/ > > posting-guide.html> > > > and provide commented, minimal, self-contained, reproducible code. > > > > > > > > > > Here's my try (please have mercy for a complete R beginner): > > > > library("kernlab") > > library("methods") > > > > # Data Definitions > > LV <- c("rPerform","rCoordCap", "rKnowGrow", "rGoalcom", "rSupport", > > &quo
Re: [R] Missing index in vector assignment
Jim & Petr, Thank you for your hint - I am really grateful, because they helped me to get one step further, and although now the problem lies somewhere else, you encouraged that we can find the solution soon! 1. To Petr's comments Petr, your hint to define y: y <- LVvar[,1, drop=FALSE] did solve the problem, so I got a data.frame with the indexes. Yet, then I turned to the call svp <- ksvm(x, y, type="nu-svc") Error in .local(x, ...) : y must be a vector or a factor. So then I followed your second advice, looking up the additional information from help file: == x is defined as: a symbolic description of the model to be fit. When not using a formula x can be a matrix or vector containing the training data or a kernel matrix of class kernelMatrix of the training data or a list of character vectors (for use with the string kernel). Note, that the intercept is always excluded, whether given in the formula or not. y is defined as a response vector with one label for each row/component of x. Can be either a factor (for classification tasks) or a numeric vector (for regression). == So I tried to convert LVvar into a matrix via as.matrix() but didn't make a difference. 2. To Jim's comments On Wed, Mar 17, 2010 at 1:10 AM, jim holtman wrote: > Please provide what LVvar is. > LVvar is a dataframe > At least provide str(LVvar), or preferably a 'dput' of the object. > str(LVvar) returns: 'data.frame': 55 obs. of 7 variables: $ rPerform : num 0.0682 -0.0682 -0.7443 0.7443 0.2619 ... $ rCoordCap: num 4.98 6.08 5.73 5.92 4.96 ... $ rKnowGrow: num 4.5 5.92 5.23 6.08 4.38 ... $ rGoalcom : num 5.81 6.58 6 5.75 5.29 ... $ rSupport : num 6.15 6.92 6.6 4.92 6 ... $ rOpcomm : num 5.98 6.25 6.33 6.5 5.29 ... $ rT2Cadap : num 5.03 6.12 4.9 6.25 5.12 ... - attr(*, "na.action")=Class 'omit' Named int 40 .. ..- attr(*, "names")= chr "40" == dput(LVvar) returns (abbreviated with ...): structure(list(rPerform = c(0.0681818181818183, -0.0681818181818183, -0.744318181818182, 0.744318181818182, 0.261931818181818, -0.900568181818182, ... rCoordCap = c(4.979167, 6.08, 5.73, 5.916667, 4.958333, ... .Names = c("rPerform", "rCoordCap", "rKnowGrow", "rGoalcom", "rSupport", "rOpcomm", "rT2Cadap"), row.names = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L, 55L, 56L), na.action = structure(40L, .Names = "40", class = "omit"), class = "data.frame") > > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. > > Here's my try (please have mercy for a complete R beginner): library("kernlab") library("methods") # Data Definitions LV <- c("rPerform","rCoordCap", "rKnowGrow", "rGoalcom", "rSupport", "rOpcomm", "rT2Cadap" ) # creates a dataframe LVvar <- na.omit(loopLV_IndexScores(LV, u_proj)) x <- (LVvar[,-1]) y <- (LVvar[,1]) svp <- ksvm(x, y, type="nu-svc") svp === Thanks so much for not giving up. Cheers, Chaehan > On Tue, Mar 16, 2010 at 6:03 PM, Chaehan So wrote: > >> Dear r-helpers, >> >> I am getting a mismatch error between two variables: >> >> svp <- ksvm(x, y, type="nu-svc") >> Error in .local(x, ...) : x and y don't match. >> >> and I suspect that it might be due to missing index in the y variable >> which >> I defined as: >> >> y <- (LVvar[,1]) >> >> I tried various methods to make the y assignment in the same format as x, >> which is a dataframe >> x <- (LVvar[,-1]) >> >> and looks like >> x >> rCoordCap rKnowGrow rGoalcom rSupport rOpcomm rT2Cadap >> 1 4.979167 4.50 5.812500 6.145833 5.979167 5.031250 >> ... >> >> but I still get y without the indexes as a vector: >> y >> [1] -1. -6.91193182 -1. 0.74431818 -6.91193182 >> >> >> Why are the results different for x and y, even though the assignment is >> the >> same >> except I exclude the columns for y? >> >> Cheers, >> Chaehan >> >>[[alternative HTML version deleted]] >> >> __ &g
[R] Missing index in vector assignment
Dear r-helpers, I am getting a mismatch error between two variables: svp <- ksvm(x, y, type="nu-svc") Error in .local(x, ...) : x and y don't match. and I suspect that it might be due to missing index in the y variable which I defined as: y <- (LVvar[,1]) I tried various methods to make the y assignment in the same format as x, which is a dataframe x <- (LVvar[,-1]) and looks like x rCoordCap rKnowGrow rGoalcom rSupport rOpcomm rT2Cadap 1 4.979167 4.50 5.812500 6.145833 5.979167 5.031250 ... but I still get y without the indexes as a vector: y [1] -1. -6.91193182 -1. 0.74431818 -6.91193182 Why are the results different for x and y, even though the assignment is the same except I exclude the columns for y? Cheers, Chaehan [[alternative HTML version deleted]] __ R-help@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.
Re: [R] Unexpected output in first iteration of for-loop
Don, that was great - thanks so much! ...and you are right rbind() being expensive, but my dataframe will always stay that small (max. twice as large). Chaehan On Thu, Feb 11, 2010 at 4:50 PM, Don MacQueen wrote: > You have a mistake in how you're setting up the object named "result" > before the loop. > > You set result <- latentVariableNames. It is a vector of length 6, when you > call the function with LV. The printout from the first iteration shows this. > > But then you rbind result with a data frame that has three columns. These > are basically incompatible, but R (apparently silently) wraps the vector of > length 6 into two rows of three columns. > > Here's an example: > > foo <- letters[1:6] >> names(foo) <- c('a','b','c') >> foo >> > abc > "a" "b" "c" "d" "e" "f" > > rbind(foo, data.frame(a='X', b='Y', c='Z',stringsAsFactors=FALSE)) >> > a b c > 1 a b c > 2 X Y Z > > What I would do is something like this (untested): > > loopCronbach <- function(latentVariableNames, groupingVariable) { > n <- length(latentVariableNames) > tmp1 <- tmp2 <- numeric(n) > > for (i in 1:n) { >tmp1[i] <- calculateIndividualCronbach(get(latentVariableNames[i])) >tmp2[i] <- calculateGroupCronbach(get(latentVariableNames[i]), > groupingVariable) > } > > result <- > data.frame(latentVariable=latentVariableNames,Indiv=tmp1,Group=tmp2) > > names(result) <- c("latentVariable", "Indiv", "Group") > result ## no need to use return() > } > > Building up a dataframe row by row using rbind() is in general an expensive > way to do things -- although in this instance it's such a small dataframe > that it doesn't matter. > > -Don > > > At 10:56 AM +0100 2/11/10, Chaehan So wrote: > >> Ok, you're right - may I rephrase: >> How should I modify the assignment of result <- latentVariableNames >> so it produces the output without the first line? >> I thought result <- NULL should do the job, but it didn't because of the >> following names(result) assignment (which I understand, but not how I >> can workaround it). >> >> Here's the output and code again: >> >> latentVariable Indiv Group >> 1 rPlanning rIterat rTDD >> 2 rPlanning0.79 0.84 >> 3rIterat0.79 0.83 >> 4 rTDD 0.9 0.96 >> 5 rStandup0.83 0.82 >> 6rRetros 0.9 0.95 >> 7rAccess0.91 0.92 >> 8 rAccTest0.87 0.9 >> >> # >> LV <- c("rPlanning", "rIterat", "rTDD", "rStandup", "rRetros", "rAccess", >> "rAccTest") >> # >> loopCronbach <- function(latentVariableNames, groupingVariable) >> { >> result <- latentVariableNames >> names(result) <- c("latentVariable", "Indiv", "Group") >> for (currentName in latentVariableNames) >> { >> print(currentName) >> print(result) >> tmp1 <- calculateIndividualCronbach(get(currentName)) >> tmp2 <- calculateGroupCronbach(get(currentName), groupingVariable) >> result <- rbind(result,data.frame(latentVariable=currentName, >> Indiv=tmp1,Group=tmp2)) >> } >> return(result) >> } >> >> On Thu, Feb 11, 2010 at 3:31 AM, jim holtman wrote: >> >> It doing exactly what you asked it to do. You have the assignment: >>> >>> result <- latentVariableNames >>> >>> and then you print it out in the loop. What were you expecting? >>> >>> On Wed, Feb 10, 2010 at 9:06 PM, Chaehan So >>> wrote: >>> >> > > Dear r-helpers, >> > > >> > > why do I get an output in the first iteration of the for-loop >> > > which contains the string values of the input vector, >> > > and how can I avoid that? >> >>> > >>> > Here's the output (only line 1 is wrong) >>> > >>> > latentVariable Indiv Group >>> > 1 rPlanning rIterat rTDD >>> > 2 rPlanning0.79 0.84 >>> > 3rIterat0.79 0.83 >>> > 4 rTDD 0.9 0.96 >>> > 5 rStandup0.83 0.82 >>> > 6rRetros 0.9 0.95 >>> > 7rAccess0.91 0.92 >>> > 8 rA
Re: [R] Unexpected output in first iteration of for-loop
Ok, you're right - may I rephrase: How should I modify the assignment of result <- latentVariableNames so it produces the output without the first line? I thought result <- NULL should do the job, but it didn't because of the following names(result) assignment (which I understand, but not how I can workaround it). Here's the output and code again: latentVariable Indiv Group 1 rPlanning rIterat rTDD 2 rPlanning0.79 0.84 3rIterat0.79 0.83 4 rTDD 0.9 0.96 5 rStandup0.83 0.82 6rRetros 0.9 0.95 7rAccess0.91 0.92 8 rAccTest0.87 0.9 # LV <- c("rPlanning", "rIterat", "rTDD", "rStandup", "rRetros", "rAccess", "rAccTest") # loopCronbach <- function(latentVariableNames, groupingVariable) { result <- latentVariableNames names(result) <- c("latentVariable", "Indiv", "Group") for (currentName in latentVariableNames) { print(currentName) print(result) tmp1 <- calculateIndividualCronbach(get(currentName)) tmp2 <- calculateGroupCronbach(get(currentName), groupingVariable) result <- rbind(result,data.frame(latentVariable=currentName, Indiv=tmp1,Group=tmp2)) } return(result) } On Thu, Feb 11, 2010 at 3:31 AM, jim holtman wrote: > It doing exactly what you asked it to do. You have the assignment: > > result <- latentVariableNames > > and then you print it out in the loop. What were you expecting? > > On Wed, Feb 10, 2010 at 9:06 PM, Chaehan So wrote: > > Dear r-helpers, > > > > why do I get an output in the first iteration of the for-loop > > which contains the string values of the input vector, > > and how can I avoid that? > > > > Here's the output (only line 1 is wrong) > > > > latentVariable Indiv Group > > 1 rPlanning rIterat rTDD > > 2 rPlanning0.79 0.84 > > 3rIterat0.79 0.83 > > 4 rTDD 0.9 0.96 > > 5 rStandup0.83 0.82 > > 6rRetros 0.9 0.95 > > 7rAccess0.91 0.92 > > 8 rAccTest0.87 0.9 > > > > # > > LV <- c("rPlanning", "rIterat", "rTDD", "rStandup", "rRetros", "rAccess", > > "rAccTest") > > # > > loopCronbach <- function(latentVariableNames, groupingVariable) > > { > > result <- latentVariableNames > > names(result) <- c("latentVariable", "Indiv", "Group") > > for (currentName in latentVariableNames) > > { > > print(currentName) > > print(result) > > tmp1 <- calculateIndividualCronbach(get(currentName)) > > tmp2 <- calculateGroupCronbach(get(currentName), groupingVariable) > > result <- rbind(result,data.frame(latentVariable=currentName, > > Indiv=tmp1,Group=tmp2)) > > } > > return(result) > > } > > > > > > a <- loopCronbach(LV, u_proj) > > > >[[alternative HTML version deleted]] > > > > __ > > R-help@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? > -- Humboldt University Berlin, Germany Institute of Psychology Rudower Chaussee 18, Room 1221 12489 Berlin Germany Office: +49 30 2093 - 9337 Mobile: +49 171- 626 9373 [[alternative HTML version deleted]] __ R-help@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.
[R] Unexpected output in first iteration of for-loop
Dear r-helpers, why do I get an output in the first iteration of the for-loop which contains the string values of the input vector, and how can I avoid that? Here's the output (only line 1 is wrong) latentVariable Indiv Group 1 rPlanning rIterat rTDD 2 rPlanning0.79 0.84 3rIterat0.79 0.83 4 rTDD 0.9 0.96 5 rStandup0.83 0.82 6rRetros 0.9 0.95 7rAccess0.91 0.92 8 rAccTest0.87 0.9 # LV <- c("rPlanning", "rIterat", "rTDD", "rStandup", "rRetros", "rAccess", "rAccTest") # loopCronbach <- function(latentVariableNames, groupingVariable) { result <- latentVariableNames names(result) <- c("latentVariable", "Indiv", "Group") for (currentName in latentVariableNames) { print(currentName) print(result) tmp1 <- calculateIndividualCronbach(get(currentName)) tmp2 <- calculateGroupCronbach(get(currentName), groupingVariable) result <- rbind(result,data.frame(latentVariable=currentName, Indiv=tmp1,Group=tmp2)) } return(result) } a <- loopCronbach(LV, u_proj) [[alternative HTML version deleted]] __ R-help@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.
Re: [R] eval function with javascript-semantics possible in R?
Dear Joe - that was wonderful :-) -Chaehan On Thu, Feb 11, 2010 at 12:44 AM, Joe Cheng wrote: > On Wed, Feb 10, 2010 at 3:37 PM, Chaehan So wrote: > >> how can I access currentName in my loop - evaluated as a variable although >> it is a string? >> > > I think you're looking for the "get" and "assign" functions. > http://pbil.univ-lyon1.fr/library/base/html/get.html > http://pbil.univ-lyon1.fr/library/base/html/assign.html > > -- Humboldt University Berlin, Germany Institute of Psychology Rudower Chaussee 18, Room 1221 12489 Berlin Germany Office: +49 30 2093 - 9337 Mobile: +49 171- 626 9373 Sent from Berlin, BE, Germany [[alternative HTML version deleted]] __ R-help@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.
Re: [R] eval function with javascript-semantics possible in R?
Ah, you were right and it was my bad (my original code was a dataframe) - but the main question remains: how can I access currentName in my loop - evaluated as a variable although it is a string? createTable <- function(latentVariableNames) { for (currentName in latentVariableNames) { doSomethingWith(currentName) } } On Wed, Feb 10, 2010 at 11:34 PM, David Winsemius wrote: > > On Feb 10, 2010, at 5:12 PM, Chaehan So wrote: > > Thank you, it works for the first problem! >> >> Yet for the second problem, how can I solve that in one dataframe (here: >> latentVariableNames), >> I store the names of the according dataframes as strings, and then want to >> access the >> according dataframe (here: currentName) - see below? >> >> latentVariables <- c("Planning", "Testing", ...) >> createTable <- function(latentVariableNames) >> { >> for (currentName in names(latentVariableNames)) >> > > ... but, but, latentVariables is a name-less vector You didn't give > those strings any names. > > > latentVariables <- c("Planning", "Testing") > > names(latentVariables) > NULL > > If you want to iterate along a character vector, you use: > > for (item in vector) { > < body of loop working on item's > > } > OR; > > for (idx in seq_along(latentVariables) ) { > < operations on latentVariables[idx] > > } > > (Review the basic manuals you are using to learn R.) > -- > David > > { >> doSomethingWith(currentName) >> } >> } >> >> >> On Wed, Feb 10, 2010 at 8:36 PM, Henrique Dallazuanna > >wrote: >> >> Try this: >>> >>> fileName <- "testfile%s" >>> lapply(sprintf(fileName, 1:2), read.csv, dec = ",") >>> >>> On Wed, Feb 10, 2010 at 5:28 PM, Chaehan So >>> wrote: >>> >>>> Dear r-helpers, >>>> >>>> I am looking for an R-equivalent for the eval-function in javascript >>>> >>> which >>> >>>> can >>>> interpret a string as code on runtime, thereby allowing things like >>>> >>>> for (i in c(1:2)) >>>> { >>>> >>>> eval(items + "i") <- read.csv(eval(filename+ i), dec=","); >>>> >>>> } >>>> >>>> which would execute (with filename="testfile"): >>>> >>>> items1 <- read.csv(testfile1, dec=","); >>>> items2 <- read.csv(testfile2, dec=","); >>>> >>>> >>>> The way I intend to use it is to define a string list containing names >>>> of >>>> date.frames, >>>> and interate this list to perform some function: >>>> >>>> createTable <- function(latentVariableNames) >>>> { >>>> for (currentName in names(latentVariableNames)) >>>> { >>>> doSomethingWith(currentName) >>>> } >>>> } >>>> >>>> >>>> Would be grateful for any help! >>>> >>>> Chaehan >>>> >>>> [[alternative HTML version deleted]] >>>> >>>> __ >>>> R-help@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. >>>> >>>> >>> >>> >>> -- >>> Henrique Dallazuanna >>> Curitiba-Paraná-Brasil >>> 25° 25' 40" S 49° 16' 22" O >>> >>> >> >> >> -- >> Humboldt University Berlin, Germany >> Institute of Psychology >> Rudower Chaussee 18, Room 1221 >> 12489 Berlin >> Germany >> Office: +49 30 2093 - 9337 >> Mobile: +49 171- 626 9373 >> >>[[alternative HTML version deleted]] >> >> __ >> R-help@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. >> > > David Winsemius, MD > Heritage Laboratories > West Hartford, CT > > -- Humboldt University Berlin, Germany Institute of Psychology Rudower Chaussee 18, Room 1221 12489 Berlin Germany Office: +49 30 2093 - 9337 Mobile: +49 171- 626 9373 Sent from Berlin, BE, Germany [[alternative HTML version deleted]] __ R-help@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.
[R] mean calculation within dataframe corrupted
Dear R-helpers, my little function below calculates the group score (tmpGroupMean) of an item, appends a "_mean" on its name and stores its value on this name. However, it does not calculate the mean of these scores (LVMean) in the same row correctly, as you can see in the below output which strangely also contains NAs. What is wrong?? Thanks for any hint!!! ## ## start groupMeanForAllItems <- function(inputDataFrame, groupVector) { # build up result dataframe starting with group means result <- data.frame(unique(groupVector)); # build up result column labels starting with group name resultNames <- "groupname"; for (itemName in names(inputDataFrame)) { # compute mean by group tmpGroupMean <- calculateGroupMean(inputDataFrame[, itemName], groupVector); result <- cbind(result, tmpGroupMean); # generate new column name newColumnName <- sprintf("%s_mean", itemName); resultNames <- c(resultNames, newColumnName) } LVMean = mean(result, na.rm=TRUE); result <- cbind(result, LVMean) resultNames <- c(resultNames, "LVMean") names(result) <- resultNames; return(result) } rPlanning <- data.frame(plan1, plan2, plan3, plan4, plan5, plan6, plan7) a <- groupMeanForAllItems(rPlanning, u_proj) print(a) ## end Output: groupname plan1_mean plan2_mean plan3_mean plan4_mean plan5_mean plan6_mean plan7_mean LVMean 1 a 4.375.000 5.625.000 5.500.000 5.250.000 6.500.000 4.375.000 5.125.000 NA 2 b 6.400.000 6.200.000 6.600.000 6.600.000 6.000.000 4.000.000 5.600.000 5.890.628 3 c 6.400.000 6.400.000 6.600.000 6.800.000 5.400.000 6.200.000 5.600.000 5.789.459 4 d 6.000.000 5.666.667 5.000.000 5.666.667 5.333.333 4.333.333 5.000.000 5.821.688 5 e 4.750.000 3.250.000 4.000.000 4.500.000 4.500.000 2.250.000 2.250.000 5.866.753 6 f 3.000.000 4.250.000 3.500.000 5.000.000 4.500.000 2.000.000 4.750.000 5.943.680 7 g 6.000.000 6.750.000 6.000.000 5.750.000 5.000.000 5.250.000 3.750.000 5.091.667 8 h 6.000.000 5.666.667 5.666.667 6.000.000 5.000.000 3.666.667 5.666.667 5.260.368 9 i 7.000.000 3.000.000 3.500.000 6.500.000 4.500.000 5.000.000 6.500.000 NA 10 j 6.400.000 5.800.000 6.400.000 6.200.000 6.600.000 5.400.000 6.200.000 5.890.628 11 k 6.000.000 6.000.000 6.000.000 4.000.000 5.000.000 6.000.000 6.000.000 5.789.459 12 l 6.500.000 6.750.000 7.000.000 6.750.000 7.000.000 6.250.000 7.000.000 5.821.688 13 m 6.666.667 6.500.000 6.666.667 6.500.000 6.500.000 5.833.333 5.500.000 5.866.753 14 n 6.571.429 6.142.857 6.142.857 5.857.143 5.714.286 5.428.571 4.714.286 5.943.680 15 o 6.666.667 6.666.667 5.666.667 4.666.667 5.666.667 4.666.667 5.000.000 5.091.667 16 p 5.857.143 6.000.000 6.142.857 5.857.143 5.571.429 5.571.429 6.142.857 5.260.368 [[alternative HTML version deleted]] __ R-help@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.
Re: [R] eval function with javascript-semantics possible in R?
Thank you, it works for the first problem! Yet for the second problem, how can I solve that in one dataframe (here: latentVariableNames), I store the names of the according dataframes as strings, and then want to access the according dataframe (here: currentName) - see below? latentVariables <- c("Planning", "Testing", ...) createTable <- function(latentVariableNames) { for (currentName in names(latentVariableNames)) { doSomethingWith(currentName) } } On Wed, Feb 10, 2010 at 8:36 PM, Henrique Dallazuanna wrote: > Try this: > > fileName <- "testfile%s" > lapply(sprintf(fileName, 1:2), read.csv, dec = ",") > > On Wed, Feb 10, 2010 at 5:28 PM, Chaehan So wrote: > > Dear r-helpers, > > > > I am looking for an R-equivalent for the eval-function in javascript > which > > can > > interpret a string as code on runtime, thereby allowing things like > > > > for (i in c(1:2)) > > { > > > > eval(items + "i") <- read.csv(eval(filename+ i), dec=","); > > > > } > > > > which would execute (with filename="testfile"): > > > > items1 <- read.csv(testfile1, dec=","); > > items2 <- read.csv(testfile2, dec=","); > > > > > > The way I intend to use it is to define a string list containing names of > > date.frames, > > and interate this list to perform some function: > > > > createTable <- function(latentVariableNames) > > { > > for (currentName in names(latentVariableNames)) > > { > > doSomethingWith(currentName) > > } > > } > > > > > > Would be grateful for any help! > > > > Chaehan > > > >[[alternative HTML version deleted]] > > > > __ > > R-help@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. > > > > > > -- > Henrique Dallazuanna > Curitiba-Paraná-Brasil > 25° 25' 40" S 49° 16' 22" O > -- Humboldt University Berlin, Germany Institute of Psychology Rudower Chaussee 18, Room 1221 12489 Berlin Germany Office: +49 30 2093 - 9337 Mobile: +49 171- 626 9373 [[alternative HTML version deleted]] __ R-help@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.
[R] eval function with javascript-semantics possible in R?
Dear r-helpers, I am looking for an R-equivalent for the eval-function in javascript which can interpret a string as code on runtime, thereby allowing things like for (i in c(1:2)) { eval(items + "i") <- read.csv(eval(filename+ i), dec=","); } which would execute (with filename="testfile"): items1 <- read.csv(testfile1, dec=","); items2 <- read.csv(testfile2, dec=","); The way I intend to use it is to define a string list containing names of date.frames, and interate this list to perform some function: createTable <- function(latentVariableNames) { for (currentName in names(latentVariableNames)) { doSomethingWith(currentName) } } Would be grateful for any help! Chaehan [[alternative HTML version deleted]] __ R-help@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.