Fill column with values from a vector when condition fullfilled

11 views
Skip to first unread message

DrunkenPhD

unread,
Jul 30, 2015, 7:36:15 AM7/30/15
to manipulatr
Hi,

My dataframe has the form as below:

City     Year       Name

a          2011

b           2011

c           2011


a           2012


b            2012

c             2012

....


I want to fill column Name with values from vector names<-c("Joh", "Mary") so for every city and every year I will have an occurrence of name.

For example I want to see :


City     Year       Name

a          2011        John

b           2011        John

c           2011        John


a           2012           John 


b            2012         John

c             2012          John


......

and so on for Mary etc..


I tried with :

df$Name[which(df$region=="a" & df$Year == 2011)]<-names2011[1:100]
df$Name[which(df$region=="b" & df$Year == 2011)]<-names2011[1:100]
df$Name[which(df$region== "c" & df$Year == 2011)]<-names2011[1:100]

but then df$Name results in list which is wrong.

Please help
Regards





Doug Mitarotonda

unread,
Jul 30, 2015, 10:32:53 AM7/30/15
to DrunkenPhD, manipulatr
See `expand.grid`

--
You received this message because you are subscribed to the Google Groups "manipulatr" group.
To unsubscribe from this group and stop receiving emails from it, send an email to manipulatr+...@googlegroups.com.
To post to this group, send email to manip...@googlegroups.com.
Visit this group at http://groups.google.com/group/manipulatr.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages