Hey folks, I'm new to the R Project so I'm facing a great problem. I've read a file into R:>myValI V L F C M A G T W S Y P H Q D N E K R 1 4,5 4,2 3,8 2,8 2,5 1,9 1,8 -0,4 -0,7 -0,9 -0,8 -1,3 -1,6 -3,2 -3,5 -3,5 -3,5 -3,5 -3,9 -4,5> mode(myVal)[1] list Now I want to multiplicate each of this values with this one:>dataA C D E F G H I K L M N P Q R S T V W Y 28 8 11 14 17 34 7 26 15 26 10 9 12 8 11 21 19 33 7 7> mode(data)[1] "numeric" Because I don't know how to do that. I had in mind to write a function, in which every single value is multiplicated with the other one. So I tried to test, if I could multiplicate two single values. This is the result:> myVal[1,1] * data[1][1] NA Warning: In Ops.factor(myVal[1, 1], data[1]) : * nicht sinnvoll f?r Faktoren (I'm using R with German language :)) What can I do, to multplicate each value with his adequate? Thanks a lot :)! dabs -- View this message in context: http://r.789695.n4.nabble.com/Beginner-Question-List-value-without-Levels-tp3547911p3547911.html Sent from the R help mailing list archive at Nabble.com.
Hi Dabs, If those are your two objects, could you post the output from: dput(myVal) dput(data) The output of dput() can be copied and pasted into the console and then we have the data on our systems exactly as you do on yours. From there it should be easy to show you how to do what you want. Cheers! Josh On Tue, May 24, 2011 at 11:54 AM, dabs <ragon016 at web.de> wrote:> Hey folks, > > I'm new to the R Project so I'm facing a great problem. I've read a file > into R: > > >>myVal > ? ?I ? V ? L ? F ? C ? M ? A ? ?G ? ?T ? ?W ? ?S ? ?Y ? ?P ? ?H ? ?Q ? ?D > N ? ?E ? ?K ? ?R > 1 4,5 4,2 3,8 2,8 2,5 1,9 1,8 -0,4 -0,7 -0,9 -0,8 -1,3 -1,6 -3,2 -3,5 -3,5 > -3,5 -3,5 -3,9 -4,5 >> mode(myVal) > ? [1] list > > > Now I want to multiplicate each of this values with this one: > >>data > ?A ?C ?D ?E ?F ?G ?H ?I ?K ?L ?M ?N ?P ?Q ?R ?S ?T ?V ?W ?Y > 28 ?8 11 14 17 34 ?7 26 15 26 10 ?9 12 ?8 11 21 19 33 ?7 ?7 >> mode(data) > [1] "numeric" > > > Because I don't know how to do that. I had in mind to write a function, in > which every single value is multiplicated with the other one. So I tried to > test, if I could multiplicate two single values. This is the result: > >> myVal[1,1] * data[1] > [1] NA > Warning: > In Ops.factor(myVal[1, 1], data[1]) : > ?* nicht sinnvoll f?r Faktoren > > > (I'm using R with German language :)) > > What can I do, to multplicate each value with his adequate? > > Thanks a lot :)! > > dabs > > > -- > View this message in context: http://r.789695.n4.nabble.com/Beginner-Question-List-value-without-Levels-tp3547911p3547911.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/
Hey Joshua, thanks for your help. Here are the dput outputs :)!>dput(myVal)structure(list(I = structure(1L, .Label = "4,5", class = "factor"), V = structure(1L, .Label = "4,2", class = "factor"), L = structure(1L, .Label = "3,8", class = "factor"), F = structure(1L, .Label = "2,8", class = "factor"), C = structure(1L, .Label = "2,5", class = "factor"), M = structure(1L, .Label = "1,9", class = "factor"), A = structure(1L, .Label = "1,8", class = "factor"), G = structure(1L, .Label = "-0,4", class = "factor"), T = structure(1L, .Label = "-0,7", class = "factor"), W = structure(1L, .Label = "-0,9", class = "factor"), S = structure(1L, .Label = "-0,8", class = "factor"), Y = structure(1L, .Label = "-1,3", class = "factor"), P = structure(1L, .Label = "-1,6", class = "factor"), H = structure(1L, .Label = "-3,2", class = "factor"), Q = structure(1L, .Label = "-3,5", class = "factor"), D = structure(1L, .Label = "-3,5", class = "factor"), N = structure(1L, .Label = "-3,5", class = "factor"), E = structure(1L, .Label = "-3,5", class = "factor"), K = structure(1L, .Label = "-3,9", class = "factor"), R = structure(1L, .Label = "-4,5", class = "factor")), .Names = c("I", "V", "L", "F", "C", "M", "A", "G", "T", "W", "S", "Y", "P", "H", "Q", "D", "N", "E", "K", "R"), class = "data.frame", row.names = c(NA, -1L)) And>dput(data)structure(c(28L, 8L, 11L, 14L, 17L, 34L, 7L, 26L, 15L, 26L, 10L, 9L, 12L, 8L, 11L, 21L, 19L, 33L, 7L, 7L), .Dim = 20L, .Dimnames structure(list( data= c("A", "C", "D", "E", "F", "G", "H", "I", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "V", "W", "Y")), .Names "data"), class = "table") Cheers! dabs -- View this message in context: http://r.789695.n4.nabble.com/Beginner-Question-List-value-without-Levels-tp3547911p3548009.html Sent from the R help mailing list archive at Nabble.com.
Hi dabs, Thanks for that. Part of your problem is that the data in myVals is not being stored as numbers, it is stored as factors. If "," is your decimal separator, then something like: myVal2 <- unlist(myVal) myVal2 <- as.numeric(levels(myVal2))[myVal2] should convert it to numeric class data, at which point, all you need to do is: myVal2 * data and because R is vectorized, each element of "myVal2" will be multiplied by each element of "data". If "," is not a decimal separator and indicates coordinates or something, then you will need to handle it a little differently. Cheers, Josh On Tue, May 24, 2011 at 12:31 PM, dabs <ragon016 at web.de> wrote:> Hey Joshua, > > thanks for your help. Here are the dput outputs :)! > >>dput(myVal) > structure(list(I = structure(1L, .Label = "4,5", class = "factor"), > ? ?V = structure(1L, .Label = "4,2", class = "factor"), L = structure(1L, > .Label = "3,8", class = "factor"), > ? ?F = structure(1L, .Label = "2,8", class = "factor"), C = structure(1L, > .Label = "2,5", class = "factor"), > ? ?M = structure(1L, .Label = "1,9", class = "factor"), A = structure(1L, > .Label = "1,8", class = "factor"), > ? ?G = structure(1L, .Label = "-0,4", class = "factor"), T = structure(1L, > .Label = "-0,7", class = "factor"), > ? ?W = structure(1L, .Label = "-0,9", class = "factor"), S = structure(1L, > .Label = "-0,8", class = "factor"), > ? ?Y = structure(1L, .Label = "-1,3", class = "factor"), P = structure(1L, > .Label = "-1,6", class = "factor"), > ? ?H = structure(1L, .Label = "-3,2", class = "factor"), Q = structure(1L, > .Label = "-3,5", class = "factor"), > ? ?D = structure(1L, .Label = "-3,5", class = "factor"), N = structure(1L, > .Label = "-3,5", class = "factor"), > ? ?E = structure(1L, .Label = "-3,5", class = "factor"), K = structure(1L, > .Label = "-3,9", class = "factor"), > ? ?R = structure(1L, .Label = "-4,5", class = "factor")), .Names = c("I", > "V", "L", "F", "C", "M", "A", "G", "T", "W", "S", "Y", "P", "H", > "Q", "D", "N", "E", "K", "R"), class = "data.frame", row.names = c(NA, > -1L)) > > > And > > >>dput(data) > structure(c(28L, 8L, 11L, 14L, 17L, 34L, 7L, 26L, 15L, 26L, 10L, > 9L, 12L, 8L, 11L, 21L, 19L, 33L, 7L, 7L), .Dim = 20L, .Dimnames > structure(list( > ? ?data= c("A", "C", "D", "E", "F", "G", "H", "I", "K", > ? ?"L", "M", "N", "P", "Q", "R", "S", "T", "V", "W", "Y")), .Names > "data"), class = "table") > > > Cheers! > > dabs > > > -- > View this message in context: http://r.789695.n4.nabble.com/Beginner-Question-List-value-without-Levels-tp3547911p3548009.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/