Re: [R] Replacing matching values by related values
Hi Works for me, how did you tested it? v <- sample(letters[1:10], 17, replace=T) d<-data.frame(letters[1:10], 1:10) d[match(v,d[,1]),2] [1] 8 2 6 1 7 10 4 10 10 9 10 8 1 8 6 7 8 Regards Petr > Předmět > > Re: [R] Replacing matching values by related values > > Thanks Michael. > I tested it and it works for numeric values, but not for the 'text' values > that I am comparing, thus comparing "a" with "a","b", etc. > Any advice how I can solve it? > > Thanks! > > > -Oorspronkelijk bericht- > Van: R. Michael Weylandt [ > mailto:michael.weyla...@gmail.com] > Verzonden: zo 18-9-2011 2:27 > Aan: Janssen, K.J.M. > CC: > Onderwerp: Re: [R] Replacing matching values by related values > > Try playing with match(). Something like > > d[match(v,d[,1]),2] > > Should work (untested bc I'm writing from my phone though) > > Michael Weylandt > > On Sep 17, 2011, at 4:33 PM, "Janssen, K.J.M." wrote: > > > > > I am trying to replace values of a vector (consisting of 15 values) by a > value that is related to a matching value in a dataset (consisting of 17 rows). > > Here's an example > > The vector: > > v <- c(f,a,e,d,m,o,e,f,i,n,e,i,b,a,o) > > > > The dataset's columns consist of the following values > > d[,1] <- c(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q) > > d[,2] <- 1:17 > > > > So I want to end up with a vector that consists of the values of the > second colomn, when the value of the vector matches the value of the first colomn. > > Thus, I aim to end up with a vector with the following values > > c(6,1,5,4,13,15,5,6,9,14,5,9,2,1,15) > > > > Help is appreciated! > > > > -- > > > > De informatie opgenomen in dit bericht kan vertrouwelijk zijn en is > > uitsluitend bestemd voor de geadresseerde. Indien u dit bericht onterecht > > ontvangt, wordt u verzocht de inhoud niet te gebruiken en de afzender direct > > te informeren door het bericht te retourneren. Het Universitair Medisch > > Centrum Utrecht is een publiekrechtelijke rechtspersoon in de zin van de W.H.W. > > (Wet Hoger Onderwijs en Wetenschappelijk Onderzoek) en staat geregistreerd bij > > de Kamer van Koophandel voor Midden-Nederland onder nr. 30244197. > > > > Denk s.v.p aan het milieu voor u deze e-mail afdrukt. > > > > -- > > > > This message may contain confidential information and is...{{dropped:12}} > > > > __ > > 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. > > __ > 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. __ 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.
Re: [R] Replacing matching values by related values
in your assignment for t3 you use nt which is undefined. thus t.n$treatment is NAs but: df<-data.frame(num=1:10,let=letters[1:10]) dat<-data.frame(let=sample(letters[1:10],20,replace=T)) dat$matched<-df$num[match(dat$let,df$let)] should get you started On Sun, Sep 18, 2011 at 7:56 AM, Janssen, K.J.M. < k.j.m.jans...@umcutrecht.nl> wrote: > Apologies, I wanted to make life easier by shortly describing my problem. > Indeed, it is better to post the full code. > I am not familiar with the dput, but I have pasted the code that I have > used below. > > > d <- matrix(NA,15,5) > d <- as.data.frame(d) > > colnames(d) <- c("studynumber","t1","t2","t[,1]","t[,2]") > > d$studynumber <- c(1:15)# add study numbers to select > studies in scenarios > d$t1 > <-c("car_pac","car_pac","cis_vin","car_pac","cis_doc","cis_gem","cis_gem","cis_vin","car_pac","car_doc","car_pac","car_pac","car_doc.pac","cis_vin","cis_iri") > d$t2 > <-c("gef","bev_car_pac","cet_cis_vin","gef","gef","bev_cis_gem","cis_pem","cet_cis_vin","car_gem_pac","car_pem","erl","cis_pac","cet_car_doc.pac","cis_doc","car_pac") > > # Link treatment to relating treatment number: make vector of all unique > treatment options > t1 <- duplicated(c(d$t1,d$t2)) # returns TRUE and False, implying that we > can need it so select > t2 <- c(d$t1,d$t2) # combine both vectors, as treatments can be both > reference as index treatment > t3 <- na.omit(ifelse(t1==FALSE,c(d$t1,d$t2),NA))[1:nt] # omit double > treatment > > #make dataset with first colomn all possible treatments, and second colomn > their respective numbers > t.n <- matrix(NA,17,2) # list possible treatments (here 17), and > link them to numbers > t.n <- as.data.frame(t.n) > colnames(t.n) <- c("treatment","numbers") > t.n$treatment <- t3 > t.n$numbers <- 1:17 > > # link treatments in d with treatment numbers in dataset t.n > > Here is where I aim to fill d$"t[,1]" and d$"t[,2]" with the corresrponding > numbers from t.n > > Thanks. > > Kristel > > > > > -Oorspronkelijk bericht- > Van: David Winsemius [mailto:dwinsem...@comcast.net] > Verzonden: zo 18-9-2011 15:20 > Aan: Janssen, K.J.M. > CC: michael.weyla...@gmail.com; r-help@r-project.org > Onderwerp: Re: [R] Replacing matching values by related values > > > On Sep 18, 2011, at 3:56 AM, Janssen, K.J.M. wrote: > > > Thanks Michael. > > I tested it and it works for numeric values, but not for the 'text' > > values that I am comparing, thus comparing "a" with "a","b", etc. > > Any advice how I can solve it? > > Solve what? You never posted full working code and an explicit > example. Unless there were actually objects named "a", "b", "c", etc. > in your workspace then the code that started out: v <- > c(f,a,e,d,m, would not have been meaningful except to hint at the > possibility that you might be comparing character vectors. I assumed > that d[,2] was actually letters[1:17] rather than what you wrote. It's > especially important to indicate whehte ryou have attached any objects. > > Post dput(head(d)) and dput(v) for the example part and include any > code use to construct them. > > -- > david. > > > > > Thanks! > > > > > > -Oorspronkelijk bericht- > > Van: R. Michael Weylandt [mailto: > michael.weyla...@gmail.com > > ] > > Verzonden: zo 18-9-2011 2:27 > > Aan: Janssen, K.J.M. > > CC: > > Onderwerp: Re: [R] Replacing matching values by related values > > > > Try playing with match(). Something like > > > > d[match(v,d[,1]),2] > > > > Should work (untested bc I'm writing from my phone though) > > > > Michael Weylandt > > > > On Sep 17, 2011, at 4:33 PM, "Janssen, K.J.M." < > k.j.m.jans...@umcutrecht.nl > > > wrote: > > > >> > >> I am trying to replace values of a vector (consisting of 15 values) > >> by a value that is related to a matching value in a dataset > >> (consisting of 17 rows). > >> Here's an example > >> The vector: > >> v <- c(f,a,e,d,m,o,e,f,i,n,e,i,b,a,o) > >> > >> The dataset's columns c
Re: [R] Replacing matching values by related values
Apologies, I wanted to make life easier by shortly describing my problem. Indeed, it is better to post the full code. I am not familiar with the dput, but I have pasted the code that I have used below. d <- matrix(NA,15,5) d <- as.data.frame(d) colnames(d) <- c("studynumber","t1","t2","t[,1]","t[,2]") d$studynumber <- c(1:15)# add study numbers to select studies in scenarios d$t1 <-c("car_pac","car_pac","cis_vin","car_pac","cis_doc","cis_gem","cis_gem","cis_vin","car_pac","car_doc","car_pac","car_pac","car_doc.pac","cis_vin","cis_iri") d$t2 <-c("gef","bev_car_pac","cet_cis_vin","gef","gef","bev_cis_gem","cis_pem","cet_cis_vin","car_gem_pac","car_pem","erl","cis_pac","cet_car_doc.pac","cis_doc","car_pac") # Link treatment to relating treatment number: make vector of all unique treatment options t1 <- duplicated(c(d$t1,d$t2)) # returns TRUE and False, implying that we can need it so select t2 <- c(d$t1,d$t2) # combine both vectors, as treatments can be both reference as index treatment t3 <- na.omit(ifelse(t1==FALSE,c(d$t1,d$t2),NA))[1:nt] # omit double treatment #make dataset with first colomn all possible treatments, and second colomn their respective numbers t.n <- matrix(NA,17,2) # list possible treatments (here 17), and link them to numbers t.n <- as.data.frame(t.n) colnames(t.n) <- c("treatment","numbers") t.n$treatment <- t3 t.n$numbers <- 1:17 # link treatments in d with treatment numbers in dataset t.n Here is where I aim to fill d$"t[,1]" and d$"t[,2]" with the corresrponding numbers from t.n Thanks. Kristel -Oorspronkelijk bericht- Van: David Winsemius [mailto:dwinsem...@comcast.net] Verzonden: zo 18-9-2011 15:20 Aan: Janssen, K.J.M. CC: michael.weyla...@gmail.com; r-help@r-project.org Onderwerp: Re: [R] Replacing matching values by related values On Sep 18, 2011, at 3:56 AM, Janssen, K.J.M. wrote: > Thanks Michael. > I tested it and it works for numeric values, but not for the 'text' > values that I am comparing, thus comparing "a" with "a","b", etc. > Any advice how I can solve it? Solve what? You never posted full working code and an explicit example. Unless there were actually objects named "a", "b", "c", etc. in your workspace then the code that started out: v <- c(f,a,e,d,m, would not have been meaningful except to hint at the possibility that you might be comparing character vectors. I assumed that d[,2] was actually letters[1:17] rather than what you wrote. It's especially important to indicate whehte ryou have attached any objects. Post dput(head(d)) and dput(v) for the example part and include any code use to construct them. -- david. > > Thanks! > > > -Oorspronkelijk bericht- > Van: R. Michael Weylandt > [mailto:michael.weyla...@gmail.com > ] > Verzonden: zo 18-9-2011 2:27 > Aan: Janssen, K.J.M. > CC: > Onderwerp: Re: [R] Replacing matching values by related values > > Try playing with match(). Something like > > d[match(v,d[,1]),2] > > Should work (untested bc I'm writing from my phone though) > > Michael Weylandt > > On Sep 17, 2011, at 4:33 PM, "Janssen, K.J.M." > wrote: > >> >> I am trying to replace values of a vector (consisting of 15 values) >> by a value that is related to a matching value in a dataset >> (consisting of 17 rows). >> Here's an example >> The vector: >> v <- c(f,a,e,d,m,o,e,f,i,n,e,i,b,a,o) >> >> The dataset's columns consist of the following values >> d[,1] <- c(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q) >> d[,2] <- 1:17 >> >> So I want to end up with a vector that consists of the values of >> the second colomn, when the value of the vector matches the value >> of the first colomn. >> Thus, I aim to end up with a vector with the following values >> c(6,1,5,4,13,15,5,6,9,14,5,9,2,1,15) >> >> Help is appreciated! >> >> -- >> >> De informatie opgenomen in dit bericht kan vertrouwelijk zijn en is >> uitsluitend bestemd voor de geadresseerde. Indien u dit bericht >> onterecht >> ontvangt, wordt u verzocht de inhoud niet te gebruiken en de >> afzender direct >> te informeren door het
Re: [R] Replacing matching values by related values
On Sep 18, 2011, at 3:56 AM, Janssen, K.J.M. wrote: Thanks Michael. I tested it and it works for numeric values, but not for the 'text' values that I am comparing, thus comparing "a" with "a","b", etc. Any advice how I can solve it? Solve what? You never posted full working code and an explicit example. Unless there were actually objects named "a", "b", "c", etc. in your workspace then the code that started out: v <- c(f,a,e,d,m, would not have been meaningful except to hint at the possibility that you might be comparing character vectors. I assumed that d[,2] was actually letters[1:17] rather than what you wrote. It's especially important to indicate whehte ryou have attached any objects. Post dput(head(d)) and dput(v) for the example part and include any code use to construct them. -- david. Thanks! -Oorspronkelijk bericht- Van: R. Michael Weylandt [mailto:michael.weyla...@gmail.com ] Verzonden: zo 18-9-2011 2:27 Aan: Janssen, K.J.M. CC: Onderwerp: Re: [R] Replacing matching values by related values Try playing with match(). Something like d[match(v,d[,1]),2] Should work (untested bc I'm writing from my phone though) Michael Weylandt On Sep 17, 2011, at 4:33 PM, "Janssen, K.J.M." > wrote: I am trying to replace values of a vector (consisting of 15 values) by a value that is related to a matching value in a dataset (consisting of 17 rows). Here's an example The vector: v <- c(f,a,e,d,m,o,e,f,i,n,e,i,b,a,o) The dataset's columns consist of the following values d[,1] <- c(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q) d[,2] <- 1:17 So I want to end up with a vector that consists of the values of the second colomn, when the value of the vector matches the value of the first colomn. Thus, I aim to end up with a vector with the following values c(6,1,5,4,13,15,5,6,9,14,5,9,2,1,15) Help is appreciated! -- De informatie opgenomen in dit bericht kan vertrouwelijk zijn en is uitsluitend bestemd voor de geadresseerde. Indien u dit bericht onterecht ontvangt, wordt u verzocht de inhoud niet te gebruiken en de afzender direct te informeren door het bericht te retourneren. Het Universitair Medisch Centrum Utrecht is een publiekrechtelijke rechtspersoon in de zin van de W.H.W. (Wet Hoger Onderwijs en Wetenschappelijk Onderzoek) en staat geregistreerd bij de Kamer van Koophandel voor Midden-Nederland onder nr. 30244197. Denk s.v.p aan het milieu voor u deze e-mail afdrukt. -- This message may contain confidential information and is... {{dropped:12}} __ 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. __ 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. David Winsemius, MD 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.
Re: [R] Replacing matching values by related values
Thanks Michael. I tested it and it works for numeric values, but not for the 'text' values that I am comparing, thus comparing "a" with "a","b", etc. Any advice how I can solve it? Thanks! -Oorspronkelijk bericht- Van: R. Michael Weylandt [mailto:michael.weyla...@gmail.com] Verzonden: zo 18-9-2011 2:27 Aan: Janssen, K.J.M. CC: Onderwerp: Re: [R] Replacing matching values by related values Try playing with match(). Something like d[match(v,d[,1]),2] Should work (untested bc I'm writing from my phone though) Michael Weylandt On Sep 17, 2011, at 4:33 PM, "Janssen, K.J.M." wrote: > > I am trying to replace values of a vector (consisting of 15 values) by a > value that is related to a matching value in a dataset (consisting of 17 > rows). > Here's an example > The vector: > v <- c(f,a,e,d,m,o,e,f,i,n,e,i,b,a,o) > > The dataset's columns consist of the following values > d[,1] <- c(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q) > d[,2] <- 1:17 > > So I want to end up with a vector that consists of the values of the second > colomn, when the value of the vector matches the value of the first colomn. > Thus, I aim to end up with a vector with the following values > c(6,1,5,4,13,15,5,6,9,14,5,9,2,1,15) > > Help is appreciated! > > -- > > De informatie opgenomen in dit bericht kan vertrouwelijk zijn en is > uitsluitend bestemd voor de geadresseerde. Indien u dit bericht onterecht > ontvangt, wordt u verzocht de inhoud niet te gebruiken en de afzender direct > te informeren door het bericht te retourneren. Het Universitair Medisch > Centrum Utrecht is een publiekrechtelijke rechtspersoon in de zin van de > W.H.W. > (Wet Hoger Onderwijs en Wetenschappelijk Onderzoek) en staat geregistreerd bij > de Kamer van Koophandel voor Midden-Nederland onder nr. 30244197. > > Denk s.v.p aan het milieu voor u deze e-mail afdrukt. > > -- > > This message may contain confidential information and is...{{dropped:12}} > > __ > 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. __ 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.
Re: [R] Replacing matching values by related values
Try playing with match(). Something like d[match(v,d[,1]),2] Should work (untested bc I'm writing from my phone though) Michael Weylandt On Sep 17, 2011, at 4:33 PM, "Janssen, K.J.M." wrote: > > I am trying to replace values of a vector (consisting of 15 values) by a > value that is related to a matching value in a dataset (consisting of 17 > rows). > Here's an example > The vector: > v <- c(f,a,e,d,m,o,e,f,i,n,e,i,b,a,o) > > The dataset's columns consist of the following values > d[,1] <- c(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q) > d[,2] <- 1:17 > > So I want to end up with a vector that consists of the values of the second > colomn, when the value of the vector matches the value of the first colomn. > Thus, I aim to end up with a vector with the following values > c(6,1,5,4,13,15,5,6,9,14,5,9,2,1,15) > > Help is appreciated! > > -- > > De informatie opgenomen in dit bericht kan vertrouwelijk zijn en is > uitsluitend bestemd voor de geadresseerde. Indien u dit bericht onterecht > ontvangt, wordt u verzocht de inhoud niet te gebruiken en de afzender direct > te informeren door het bericht te retourneren. Het Universitair Medisch > Centrum Utrecht is een publiekrechtelijke rechtspersoon in de zin van de > W.H.W. > (Wet Hoger Onderwijs en Wetenschappelijk Onderzoek) en staat geregistreerd bij > de Kamer van Koophandel voor Midden-Nederland onder nr. 30244197. > > Denk s.v.p aan het milieu voor u deze e-mail afdrukt. > > -- > > This message may contain confidential information and is...{{dropped:12}} > > __ > 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. __ 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.