Hi everybody,
I have just thought that it might help if I would provide a sample data
set. I have attached it as a txt file (tab separated).
Here is the "modified" code to fit this data set.
----
library(WRS)
file <- read.table(file=file.choose(), header=T, sep="\t")
seq.num<- c(4,5) #column (variable) indexes to be used as
numerical variables
# fac2list() separates the data from file[,k] into groups from levels in
file[3] and store into list mode.
for(i in 1:length(seq.num)) {
k<- seq.num[i]
name.num<- names(file)[k]
assign(paste(names(file)[3], name.num, sep="_"), fac2list(file[,k],
file[3]))
names(paste(names(file)[3], name.num, sep="_"))<-
levels(factor(file[[3]])) #that line doesn't work, but I would like
something in this direction,
}
#I couldn't make it work with get() either
----
Desired output (I named it "manually"):
> TO_POS_Asfc.median
$M1
[1] 143.2065 180.0317 121.6397 175.0321 196.7545 208.3876
$M2
[1] 83.76985 190.26124 35.94771 158.67638 228.32178 247.06147 53.97887
[8] 218.60500 210.73514 167.37230 311.49512
$M3
[1] 316.37916 238.75643 277.44513 142.00038 242.58559 285.64139 61.01016
[8] 353.16336 150.75096 300.15989 298.99994 74.33973
$P3
[1] 140.9876 207.7029
$P4
[1] 316.71972 120.76090 85.56545 135.87049 290.98684 160.52683
> TO_POS_Smc.median
$M1
[1] 6.411332 31.347971 5.649655 8.954671 8.279096 10.385299
$M2
[1] 9.358691 18.657771 3.033461 11.193338 10.385299 11.233078 2.417496
[8] 10.021019 10.100497 19.796975 19.459188
$M3
[1] 27.691922 13.895635 26.824272 17.909342 19.128024 21.730973 6.206010
[8] 24.214702 11.531125 13.617458 19.717496 9.305704
$P3
[1] 9.669985 18.392840
$P4
[1] 18.346478 10.504518 8.967917 4.291884 23.380168 9.014280
Thanks in advance
Ivan
Le 2/3/2010 10:22, Ivan Calandra a écrit :
Hi!
Looks like get() doesn't work. Here is what I do and what I get (still
in the loop):
names(get(paste(names(file)[3], name.num, sep="_")))<-
levels(factor(file[[3]]))
Error in names(get(paste(names(file)[3], name.num, sep = "_")))<-
levels(factor(file[[3]])) :
could not find function "get<-"
print(names(get(paste(names(file)[3], name.num, sep="_")))) #4
iterations; I wanted to test this part
NULL
NULL
NULL
NULL
Does anyone has an idea about it?
Thanks
Ivan
Le 2/2/2010 19:38, Chris Campbell a écrit :
On Tue, Feb 2, 2010 at 11:44, Ivan Calandra
<ivan.calan...@uni-hamburg.de> wrote:
Hi David,
Thanks for your answer.
But I don't really see how I can extend it with my real data.
The thing is that I have more than 3 names and 1 value for each name.
Moreover, each is different from one run to another. That is why I was
trying with a modification of names(). Also to be noted is that I
simplified
the "name" in assign(); I actually have 2 other variables that will be
pasted to create the name.
Here is my code (I kept only what is important for that part):
----
library(WRS)
seq.num<- seq(7,10,1) #column (variable) indexes to be used as
numerical variables
# fac2list() separates the data from file[,k] into groups from
levels in
file[3] and store into list mode.
for(i in 1:length(seq.num)) {
k<- seq.num[i]
name.num<- names(file)[k]
assign(paste(names(file)[3], name.num, sep="_"), fac2list(file[,k],
file[3]))
names(paste(names(file)[3], name.num, sep="_"))<-
levels(factor(file[[3]])) #that line doesn't work, but I would like
something in this direction
}
----
Sounds like a job for 'get'. Try this (untested):
names(get(paste(names(file)[3], name.num, sep="_")))
Good luck
Thanks in advance for your help.
Regards,
Ivan
Le 2/1/2010 18:47, David Winsemius a écrit :
On Feb 1, 2010, at 12:33 PM, Ivan Calandra wrote:
I have a follow-up question:
I use assign() to store some value in my paste()-created object as
suggested:
for (i in 1:3) {
assign(paste("object", i, sep=""), c("a", "b", "c"))
}
Then I would like to change the names of the elements of that object
within the loop. Since it is all in a loop, I cannot give the name
of the
object manually by doing something like: names(object1)<- c("tooth",
"bone", "species").
The only thing I can give to names() is paste("object", i,
sep=""), which
doesn't work.
Any idea of how to do it?
for (i in paste("object", 1:3, sep="")) {
+ assign(i, c("tooth"="a", "bone"="b", "species"="c") )
+ }
object1
tooth bone species
"a" "b" "c"
Thanks in advance
Ivan
Le 2/1/2010 17:14, David Winsemius a écrit :
Upon reading it yesterday, it appeared as it would have required
some
serious testing and there was no data on which to do any work.
You were
clearly not taking the time to isolate the problem and construct
a dataset.
But who knows? When you say "What I want to do is. ... ,I would
like the
name of the list to be created in the loop too", maybe all you
needed was to
be pointed to was:
?assign
But if that were the case, then you lost most of your audience
along the
way with a bunch of unneeded and obscure code.
David Winsemius, MD
Heritage Laboratories
West Hartford, CT
______________________________________________
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.
--
Ivan CALANDRA
PhD Student
University of Hamburg
Biozentrum Grindel und Zoologisches Institut und Museum
Martin-Luther-King-Platz 3
D-20146 Hamburg, GERMANY
+49(0)40 42838 6231
ivan.calan...@uni-hamburg.de
**********
http://www.for771.uni-bonn.de
http://webapp5.rrz.uni-hamburg.de/mammals/eng/mitarbeiter.php
SPECSHOR BONE TO_POS Asfc.median Smc.median
cotau tm M1 143.20655 6.411332
cotau tm M1 180.0317145 31.347971
cotau tm M1 121.639695 5.6496555
cotau tm M1 175.032101 8.954671
cotau tm M1 196.754496 8.279096
cotau tm M1 208.387571 10.385299
cotau tm M2 83.7698535 9.358691
cotau tm M2 190.261243 18.6577715
cotau tm M2 35.94771 3.033461
cotau tm M2 158.676376 11.1933385
cotau tm M2 228.321783 10.385299
cotau tm M2 247.0614745 11.233078
cotau tm M2 53.9788715 2.4174965
cotau tm M2 218.604999 10.0210185
cotau tm M2 210.735137 10.1004975
cotau tm M2 167.3723045 19.7969755
cotau tm M2 311.495118 19.459188
cotau tm M3 316.3791595 27.691922
cotau tm M3 238.75643 13.8956355
cotau tm M3 277.445134 26.824272
cotau tm M3 142.000378 17.9093415
cotau tm M3 242.585585 19.128024
cotau tm M3 285.6413885 21.7309725
cotau tm M3 61.010164 6.2060105
cotau tm M3 353.163364 24.2147015
cotau tm M3 150.750955 11.5311255
cotau tm M3 300.159889 13.617458
cotau tm M3 298.999937 19.717496
cotau tm M3 74.339725 9.3057045
cotau tm P3 140.9875625 9.669985
cotau tm P3 207.70291 18.3928405
cotau tm P4 316.719723 18.346478
cotau tm P4 120.7608955 10.5045175
cotau tm P4 85.5654495 8.967917
cotau tm P4 135.870486 4.291884
cotau tm P4 290.9868405 23.3801685
cotau tm P4 160.526826 9.0142805
______________________________________________
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.