Hello R Users,
I have vectors
x <- c("a2","b7","c8")
y1 <- c(1,2,3,2)
y2 <- c(4,2,7,5,4,3,8)
y3 <- c(1:10)
and I want to assign values form vector y1 to a new variable which
name comes from the 1st value of the vector x etc. How to do it using
only vector x. As a result I should have:
a2 <- y1
b7 <- y2
c8 <- y3
More general problem: I have 2 txt files.
First is a one column
C4
C6
F
D
D1
D2
D5
D6
....
Second is (non-define number of columns):
44, 60, 65, 139, 138
47
48
49, 46
.....
is it possible to automatically assign:
C4 <- c(44, 60, 65, 139, 138)
C6 <- c(47)
...
I would appreciate for any help.
Robert
Hi Robert,
What about a named list? This will generalize to your two text files
one with data, the other with names.
x <- c("a2","b7","c8")
y1 <- c(1,2,3,2)
y2 <- c(4,2,7,5,4,3,8)
y3 <- c(1:10)
dat <- list(c(1,2,3,2), c(4,2,7,5,4,3,8), c(1:10))
names(dat) <- c("a2","b7","c8")
dat
# example
dat$a2
dat[["a2"]]
## or using your variables
dat2 <- list(y1, y2, y3)
names(dat2) <- x
Cheers,
Josh
On Fri, Nov 19, 2010 at 2:57 AM, Robert Ruser <robert.ruser at gmail.com>
wrote:> Hello R Users,
> I have vectors
> x <- c("a2","b7","c8")
> y1 <- c(1,2,3,2)
> y2 <- c(4,2,7,5,4,3,8)
> y3 <- c(1:10)
> and I want to assign values form vector y1 to a new variable which
> name comes from the 1st value of the vector x etc. How to do it using
> only vector x. ?As a result I should have:
> a2 <- y1
> b7 <- y2
> c8 <- y3
>
> More general problem: I have 2 txt files.
> First is a one column
> C4
> C6
> F
> D
> D1
> D2
> D5
> D6
> ....
> Second is (non-define number of columns):
> 44, 60, 65, 139, 138
> 47
> 48
> 49, 46
> .....
> is it possible to automatically assign:
> C4 <- c(44, 60, 65, 139, 138)
> C6 <- c(47)
> ...
>
> I would appreciate for any help.
>
> Robert
>
> ______________________________________________
> 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/
Hi: On Fri, Nov 19, 2010 at 2:57 AM, Robert Ruser <robert.ruser@gmail.com>wrote:> Hello R Users, > I have vectors > x <- c("a2","b7","c8") > y1 <- c(1,2,3,2) > y2 <- c(4,2,7,5,4,3,8) > y3 <- c(1:10) > and I want to assign values form vector y1 to a new variable which > name comes from the 1st value of the vector x etc. How to do it using > only vector x. As a result I should have: > a2 <- y1 > b7 <- y2 > c8 <- y3 >l <- list(y1, y2, y3) for(i in seq_along(x)) assign(x[i], l[[i]])> > More general problem: I have 2 txt files. > First is a one column > C4 > C6 > F > D > D1 > D2 > D5 > D6 > .... > Second is (non-define number of columns): > 44, 60, 65, 139, 138 > 47 > 48 > 49, 46 > ..... > is it possible to automatically assign: > C4 <- c(44, 60, 65, 139, 138) > C6 <- c(47) > ... > > Same concept as above.HTH, Dennis> I would appreciate for any help. > > Robert > > ______________________________________________ > 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. >[[alternative HTML version deleted]]
Try this: mapply(assign, x, list(y1, y2, y3), MoreArgs = list(envir = globalenv())) On Fri, Nov 19, 2010 at 8:57 AM, Robert Ruser <robert.ruser@gmail.com>wrote:> Hello R Users, > I have vectors > x <- c("a2","b7","c8") > y1 <- c(1,2,3,2) > y2 <- c(4,2,7,5,4,3,8) > y3 <- c(1:10) > and I want to assign values form vector y1 to a new variable which > name comes from the 1st value of the vector x etc. How to do it using > only vector x. As a result I should have: > a2 <- y1 > b7 <- y2 > c8 <- y3 > > More general problem: I have 2 txt files. > First is a one column > C4 > C6 > F > D > D1 > D2 > D5 > D6 > .... > Second is (non-define number of columns): > 44, 60, 65, 139, 138 > 47 > 48 > 49, 46 > ..... > is it possible to automatically assign: > C4 <- c(44, 60, 65, 139, 138) > C6 <- c(47) > ... > > I would appreciate for any help. > > Robert > > ______________________________________________ > 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 [[alternative HTML version deleted]]