I have two datasets, the first has this shape (each word is a column) Name address phone .. .. The second one has the following shape Name request I need a contingency table with for example phone and request. The people registered in these datasets are present in both datasets, BUT in the first every record is a person, so every person is counted once and is 1 row, in the second every row is a request, so if a person has requested 1 thing this person is present only once, but is the person has requested 3 things, this person we'll be present 3 times. ?? I now that to use access could be the solution, but in this moment the computer expert give me data only as .xls/.csv data. This creates the possibility of two different cross tab, I really need both, the first has as total count the number of people The second one has as total count the number of request -- View this message in context: http://r.789695.n4.nabble.com/Interweaving-of-two-datasets-tp4608505.html Sent from the R help mailing list archive at Nabble.com.
Interweaving of two datasets
11 messages · lunarossa, Rui Barradas, John Kane
Hello, lunarossa wrote
I have two datasets, the first has this shape (each word is a column) Name address phone .. .. The second one has the following shape Name request I need a contingency table with for example phone and request. The people registered in these datasets are present in both datasets, BUT in the first every record is a person, so every person is counted once and is 1 row, in the second every row is a request, so if a person has requested 1 thing this person is present only once, but is the person has requested 3 things, this person we'll be present 3 times. ?? I now that to use access could be the solution, but in this moment the computer expert give me data only as .xls/.csv data. This creates the possibility of two different cross tab, I really need both, the first has as total count the number of people The second one has as total count the number of request
Try ?merge set.seed(1) df1 <- data.frame(Name=LETTERS[1:5], Phone=1:5) df2 <- data.frame(Name=sample(LETTERS[1:5], 20, TRUE), Request=sample(letters[1:3], 20, TRUE)) joined <- merge(df1, df2) with(joined, table(Name, Request)) Also, I hope this is what you want, it corresponds to your description but there's no "reproducible example (posting-guide)". Rui Barradas -- View this message in context: http://r.789695.n4.nabble.com/Interweaving-of-two-datasets-tp4608505p4608740.html Sent from the R help mailing list archive at Nabble.com.
5 days later
it doesn't work. Find attached what I need explained in xls. Thank you very very much! http://r.789695.n4.nabble.com/file/n4622912/interweaving_of_2_datasets.xls interweaving_of_2_datasets.xls -- View this message in context: http://r.789695.n4.nabble.com/Interweaving-of-two-datasets-tp4608505p4622912.html Sent from the R help mailing list archive at Nabble.com.
Hello,
What doesn't work, exactly?
I can only see two things:
1. The order of the columns is different, first data.frame in merge
instruction comes first.
Solution: reverse the order of data.frames in merge.
2. The order of the rows is different, the merge function orders it's output
by the common col(s).
Solutions: leave as is or use 'order'.
# first is the first sheet in attached .xls file, second is the second and
result is the third.
# 1.
res2 <- merge(second, first)
# 2.
result[order(result$name, result$request), ]
res2[order(res2$name, res2$request), ]
If this isn't it, please state what the problem is, "doesn't work" is a bit
vague.
Hope this helps,
Rui Barradas
lunarossa wrote
it doesn't work. Find attached what I need explained in xls. Thank you very very much! http://r.789695.n4.nabble.com/file/n4622912/interweaving_of_2_datasets.xls interweaving_of_2_datasets.xls
-- View this message in context: http://r.789695.n4.nabble.com/Interweaving-of-two-datasets-tp4608505p4623624.html Sent from the R help mailing list archive at Nabble.com.
Hi Ruri, sorry for my vagueness. The problem is not the order. I tried to merge the datasets as you wrote. The result is that when I input: table(phone) The output is: 0 And this happens for all the variables. -- View this message in context: http://r.789695.n4.nabble.com/Interweaving-of-two-datasets-tp4608505p4623672.html Sent from the R help mailing list archive at Nabble.com.
With the sample data and the merge as above,
table(res2$phone)
454 2123 2342 9876 56775 87678
2 2 3 3 4 1
with(res2, table(name, request))
request
name book cigarettes drink food paper
Andy 0 1 0 0 0
Bruce 0 0 0 2 2
Ella 1 0 1 1 0
John 1 0 0 0 1
Luna 0 0 0 1 1
Morgana 1 1 0 1 0
Rui Barradas
lunarossa wrote
Hi Rui, sorry for my vagueness. The problem is not the order. I tried to merge the datasets as you wrote. The result is that when I input: table(phone) The output is: 0 And this happens for all the variables.
-- View this message in context: http://r.789695.n4.nabble.com/Interweaving-of-two-datasets-tp4608505p4623724.html Sent from the R help mailing list archive at Nabble.com.
I don't quite follow what you are doing. Here is what Rui was suggesting using your sample data
===================================================================
dat1 <- structure(list(name = structure(c(4L, 3L, 5L, 2L, 1L, 6L), .Label = c("Andy",
"Bruce", "Ella", "John", "Luna", "Morgana"), class = "factor"),
address = structure(c(2L, 4L, 1L, 3L, 2L, 4L), .Label = c("Kiev street",
"london road", "main avenue", "Rome street"), class = "factor"),
phone = c(2123L, 2342L, 454L, 56775L, 287678L, 39876L)), .Names = c("name",
"address", "phone"), class = "data.frame", row.names = c(NA,
-6L))
dat2 <- structure(list(name = structure(c(4L, 4L, 3L, 3L, 3L, 5L, 5L,
2L, 2L, 2L, 2L, 1L, 6L, 6L, 6L), .Label = c("Andy", "Bruce",
"Ella", "John", "Luna", "Morgana"), class = "factor"), request = structure(c(1L,
5L, 4L, 1L, 3L, 4L, 5L, 5L, 5L, 4L, 4L, 2L, 1L, 4L, 2L), .Label = c("book",
"cigarettes", "drink", "food", "paper"), class = "factor")), .Names = c("name",
"request"), class = "data.frame", row.names = c(NA, -15L))
mydata <- merge(dat1, dat2)
mydata
===================================================================
#If you need information about a variable in the merge try something like:
table(mydata$phone)
Is this of any help?
John Kane
Kingston ON Canada
-----Original Message----- From: gloriaalbe1 at yahoo.it Sent: Thu, 10 May 2012 07:33:45 -0700 (PDT) To: r-help at r-project.org Subject: Re: [R] Interweaving of two datasets Hi Ruri, sorry for my vagueness. The problem is not the order. I tried to merge the datasets as you wrote. The result is that when I input: table(phone) The output is: 0 And this happens for all the variables. -- View this message in context: http://r.789695.n4.nabble.com/Interweaving-of-two-datasets-tp4608505p4623672.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.
____________________________________________________________ GET FREE SMILEYS FOR YOUR IM & EMAIL - Learn more at http://www.inbox.com/smileys Works with AIM?, MSN? Messenger, Yahoo!? Messenger, ICQ?, Google Talk? and most webmails
I tried to do using merge, I think now it works (anyway it is your worth), I
wrote:
df1<-read.csv("df1.csv",head=T)
attach(df1)
df2<-read.csv("df2.csv",head=T)
attach(df2)
join<-merge(df1,df2,by.x="name",by.y="name")
Is it correct? Coz I'm very frightened to lose datas or make mistakes
--
View this message in context: http://r.789695.n4.nabble.com/Interweaving-of-two-datasets-tp4608505p4624852.html
Sent from the R help mailing list archive at Nabble.com.
Hello,
I'm very frightened to lose datas or make mistakes
You wont lose rows, don't worry. Two notes: merge uses the common columns in both data.frames, so by.x and by.y aren't really needed but they wont hurt. And second, from the help page for 'attach', "attach can lead to confusion. " That's why I've used 'with' See it's help page ?with (Or the way it was used.) Rui Barradas lunarossa wrote
I tried to do using merge, I think now it works (anyway it is your worth),
I wrote:
df1<-read.csv("df1.csv",head=T)
attach(df1)
df2<-read.csv("df2.csv",head=T)
attach(df2)
join<-merge(df1,df2,by.x="name",by.y="name")
Is it correct? Coz I'm very frightened to lose datas or make mistakes
-- View this message in context: http://r.789695.n4.nabble.com/Interweaving-of-two-datasets-tp4608505p4624958.html Sent from the R help mailing list archive at Nabble.com.
Ok, merging in this way the total number of rows of the new dataset is the number of rows of the dataset "request". Is it possible to obtain a new dataset with the number of rows of the dataset "phone"? -- View this message in context: http://r.789695.n4.nabble.com/Interweaving-of-two-datasets-tp4608505p4626591.html Sent from the R help mailing list archive at Nabble.com.
1 day later
I'm not sure if this is what you want but try
library(reshape2)
dat3 <- merge(dat1, dat2)
xx <- melt(dat3, id=c("phone", "request"))
dcast(dat3 , phone + name + address ~ request )
John Kane
Kingston ON Canada
-----Original Message----- From: gloriaalbe1 at yahoo.it Sent: Fri, 11 May 2012 07:29:22 -0700 (PDT) To: r-help at r-project.org Subject: Re: [R] Interweaving of two datasets Ok, merging in this way the total number of rows of the new dataset is the number of rows of the dataset "request". Is it possible to obtain a new dataset with the number of rows of the dataset "phone"? -- View this message in context: http://r.789695.n4.nabble.com/Interweaving-of-two-datasets-tp4608505p4626591.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.
____________________________________________________________ Send your photos by email in seconds... TRY FREE IM TOOLPACK at http://www.imtoolpack.com/default.aspx?rc=if3 Works in all emails, instant messengers, blogs, forums and social networks.