Dear r-users, I have these data below: I would like to compare each column with a certain value and count how many in each column less than that specified value. odd column (1,3,5) will compare with 1.61 and even column (2,4,6) will compare with 75 and I would like to count how many for each column. I tried these below but it give me just one set of data. critical <- c(1.61,75.89, 1.61, 75.89, 1.61, 75.89) ; critical pilih <- mydata[mydata < critical] ; pilih jumlah <- length(pilih) ; jumlah kuasa <- 1 - (jumlah/1000); kuasa> head(mydata,20)V1 V1 V1 V1 1 3.014505 62.96425 3.014505 138.0673 2 2.817503 56.03400 2.817503 133.3411 3 2.976227 47.12192 2.976227 139.2438 4 2.825495 75.05284 2.825495 129.2959 5 2.793500 52.75190 2.793500 130.9874 6 3.006333 54.56210 3.006333 136.2982 7 3.245833 71.31358 3.245833 150.0974 8 2.559980 64.28300 2.559980 114.5395 9 2.548456 69.48727 2.548456 118.7383 10 2.715040 58.64134 2.715040 123.0574 11 2.799652 58.96281 2.799652 126.9216 12 2.900839 75.43427 2.900839 131.6100 13 3.183844 63.57657 3.183844 143.5530 14 2.792940 63.04534 2.792940 120.1010 15 3.229830 71.09790 3.229830 136.6931 16 3.528115 68.64850 3.528115 156.0135 17 2.973711 58.45141 2.973711 128.6738 18 2.518891 53.62788 2.518891 110.6072 19 2.848493 79.29142 2.848493 127.6110 20 2.971362 57.81008 2.971362 129.5411 [[alternative HTML version deleted]]
HI, Your example data had only 4 columns mydata<-read.table(text=" ? 3.014505 62.96425 3.014505 138.0673 ? 2.817503 56.03400 2.817503 133.3411 ? 2.976227 47.12192 2.976227 139.2438 ? 2.825495 75.05284 2.825495 129.2959 ? 2.793500 52.75190 2.793500 130.9874 ? 3.006333 54.56210 3.006333 136.2982 ? 3.245833 71.31358 3.245833 150.0974 ? 2.559980 64.28300 2.559980 114.5395 ? 2.548456 69.48727 2.548456 118.7383 ?2.715040 58.64134 2.715040 123.0574 ?2.799652 58.96281 2.799652 126.9216 ?2.900839 75.43427 2.900839 131.6100 ?3.183844 63.57657 3.183844 143.5530 ?2.792940 63.04534 2.792940 120.1010 ?3.229830 71.09790 3.229830 136.6931 ?3.528115 68.64850 3.528115 156.0135 ?2.973711 58.45141 2.973711 128.6738 ?2.518891 53.62788 2.518891 110.6072 ?2.848493 79.29142 2.848493 127.6110 ?2.971362 57.81008 2.971362 129.5411 ",sep="",header=F) critical<-c(1.61,75.89) res<-unlist(lapply(1:2,function(i) colSums(mydata[,seq(i,ncol(mydata),by=2)]<critical[i]))) ?res #V1 V3 V2 V4 # 0? 0 19? 0 A.K. ----- Original Message ----- From: Roslina Zakaria <zroslina at yahoo.com> To: "r-help at r-project.org" <r-help at r-project.org> Cc: Sent: Monday, January 21, 2013 12:39 AM Subject: [R] compare and count data Dear r-users, ? I have these data below: ? I would like to compare each column with a certain value and count how many in each column less than that specified value.? ? odd column (1,3,5)?will compare with 1.61 and even column (2,4,6)?will compare with 75 and I would like to count how many for each column.? ? ? I tried these below but it give me just one set of data. ? critical? <- c(1.61,75.89, 1.61, 75.89, 1.61, 75.89)?? ; critical pilih <- mydata[mydata < critical] ; pilih jumlah <- length(pilih)?? ; jumlah kuasa <- 1 - (jumlah/1000);? kuasa ?> head(mydata,20)???????? V1?????? V1?????? V1?????? V1 1? 3.014505 62.96425 3.014505 138.0673 2? 2.817503 56.03400 2.817503 133.3411 3? 2.976227 47.12192 2.976227 139.2438 4? 2.825495 75.05284 2.825495 129.2959 5? 2.793500 52.75190 2.793500 130.9874 6? 3.006333 54.56210 3.006333 136.2982 7? 3.245833 71.31358 3.245833 150.0974 8? 2.559980 64.28300 2.559980 114.5395 9? 2.548456 69.48727 2.548456 118.7383 10 2.715040 58.64134 2.715040 123.0574 11 2.799652 58.96281 2.799652 126.9216 12 2.900839 75.43427 2.900839 131.6100 13 3.183844 63.57657 3.183844 143.5530 14 2.792940 63.04534 2.792940 120.1010 15 3.229830 71.09790 3.229830 136.6931 16 3.528115 68.64850 3.528115 156.0135 17 2.973711 58.45141 2.973711 128.6738 18 2.518891 53.62788 2.518891 110.6072 19 2.848493 79.29142 2.848493 127.6110 20 2.971362 57.81008 2.971362 129.5411 ??? [[alternative HTML version deleted]] ______________________________________________ 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.
On 01/21/2013 04:39 PM, Roslina Zakaria wrote:> Dear r-users, > > I have these data below: > > I would like to compare each column with a certain value and count how many in each column less than that specified value. > > odd column (1,3,5) will compare with 1.61 and even column (2,4,6) will compare with 75 and I would like to count how many for each column. > > > I tried these below but it give me just one set of data. > > critical<- c(1.61,75.89, 1.61, 75.89, 1.61, 75.89) ; critical > pilih<- mydata[mydata< critical] ; pilih > jumlah<- length(pilih) ; jumlah > kuasa<- 1 - (jumlah/1000); kuasa > >> head(mydata,20) > V1 V1 V1 V1 > 1 3.014505 62.96425 3.014505 138.0673 > 2 2.817503 56.03400 2.817503 133.3411 > 3 2.976227 47.12192 2.976227 139.2438 > 4 2.825495 75.05284 2.825495 129.2959 > 5 2.793500 52.75190 2.793500 130.9874 > 6 3.006333 54.56210 3.006333 136.2982 > 7 3.245833 71.31358 3.245833 150.0974 > 8 2.559980 64.28300 2.559980 114.5395 > 9 2.548456 69.48727 2.548456 118.7383 > 10 2.715040 58.64134 2.715040 123.0574 > 11 2.799652 58.96281 2.799652 126.9216 > 12 2.900839 75.43427 2.900839 131.6100 > 13 3.183844 63.57657 3.183844 143.5530 > 14 2.792940 63.04534 2.792940 120.1010 > 15 3.229830 71.09790 3.229830 136.6931 > 16 3.528115 68.64850 3.528115 156.0135 > 17 2.973711 58.45141 2.973711 128.6738 > 18 2.518891 53.62788 2.518891 110.6072 > 19 2.848493 79.29142 2.848493 127.6110 > 20 2.971362 57.81008 2.971362 129.5411 > [[alternative HTML version deleted]] > >Hi Roslina, I think what you may want is something like this: colSums(t(apply(mydata,1,FUN="<",rep(c(1.61,75), length.out=dim(mydata)[2]/2)))) Jim
Hi Roslina, No problem. If you take out the i from the code, 1-(res/1000) #?? V1??? V3??? V2??? V4 #1.000 1.000 0.981 1.000 Is it what you were looking for? Also, in addition, you could also try: ? #if the total column number is even critical<-rep(c(1.61,75.89),ncol(mydata)/2) #if it is odd, critical1<-c(rep(c(1.61,75.89),(ncol(mydata)-1)/2),1.61) colSums(mapply("<",mydata,critical)) #V1 V2 V3 V4 # 0 19? 0? 0 A.K. ________________________________ From: Roslina Zakaria <zroslina at yahoo.com> To: arun <smartpink111 at yahoo.com> Sent: Monday, January 21, 2013 2:45 AM Subject: Re: [R] compare and count data Hi Arun, Thank you so much for your fast respond.? Now I would like to use the 'res' value and calculate using the formula below: power?<- 1 - (res(i) /1000);? power I tried the above code but it gives me this message: power <- function(1 - (res(i) /1000)); Error: unexpected numeric constant in "kuasa <- function(1 Thank you so much Arun for your help. From: arun <smartpink111 at yahoo.com> To: Roslina Zakaria <zroslina at yahoo.com> Cc: R help <r-help at r-project.org> Sent: Monday, January 21, 2013 2:12 PM Subject: Re: [R] compare and count data HI, Your example data had only 4 columns mydata<-read.table(text=" ? 3.014505 62.96425 3.014505 138.0673 ? 2.817503 56.03400 2.817503 133.3411 ? 2.976227 47.12192 2.976227 139.2438 ? 2.825495 75.05284 2.825495 129.2959 ? 2.793500 52.75190 2.793500 130.9874 ? 3.006333 54.56210 3.006333 136.2982 ? 3.245833 71.31358 3.245833 150.0974 ? 2.559980 64.28300 2.559980 114.5395 ? 2.548456 69.48727 2.548456 118.7383 ?2.715040 58.64134 2.715040 123.0574 ?2.799652 58.96281 2.799652 126.9216 ?2.900839 75.43427 2.900839 131.6100 ?3.183844 63.57657 3.183844 143.5530 ?2.792940 63.04534 2.792940 120.1010 ?3.229830 71.09790 3.229830 136.6931 ?3.528115 68.64850 3.528115 156.0135 ?2.973711 58.45141 2.973711 128.6738 ?2.518891 53.62788 2.518891 110.6072 ?2.848493 79.29142 2.848493 127.6110 ?2.971362 57.81008 2.971362 129.5411 ",sep="",header=F) critical<-c(1.61,75.89) res<-unlist(lapply(1:2,function(i) colSums(mydata[,seq(i,ncol(mydata),by=2)]<critical[i]))) ?res #V1 V3 V2 V4 # 0? 0 19? 0 A.K. ----- Original Message ----- From: Roslina Zakaria <zroslina at yahoo.com> To: "r-help at r-project.org" <r-help at r-project.org> Cc: Sent: Monday, January 21, 2013 12:39 AM Subject: [R] compare and count data Dear r-users, I have these data below: I would like to compare each column with a certain value and count how many in each column less than that specified value.? odd column (1,3,5)?will compare with 1.61 and even column (2,4,6)?will compare with 75 and I would like to count how many for each column.? I tried these below but it give me just one set of data. critical? <- c(1.61,75.89, 1.61, 75.89, 1.61, 75.89)?? ; critical pilih <- mydata[mydata < critical] ; pilih jumlah <- length(pilih)?? ; jumlah kuasa <- 1 - (jumlah/1000);? kuasa> head(mydata,20)???????? V1?????? V1?????? V1?????? V1 1? 3.014505 62.96425 3.014505 138.0673 2? 2.817503 56.03400 2.817503 133.3411 3? 2.976227 47.12192 2.976227 139.2438 4? 2.825495 75.05284 2.825495 129.2959 5? 2.793500 52.75190 2.793500 130.9874 6? 3.006333 54.56210 3.006333 136.2982 7? 3.245833 71.31358 3.245833 150.0974 8? 2.559980 64.28300 2.559980 114.5395 9? 2.548456 69.48727 2.548456 118.7383 10 2.715040 58.64134 2.715040 123.0574 11 2.799652 58.96281 2.799652 126.9216 12 2.900839 75.43427 2.900839 131.6100 13 3.183844 63.57657 3.183844 143.5530 14 2.792940 63.04534 2.792940 120.1010 15 3.229830 71.09790 3.229830 136.6931 16 3.528115 68.64850 3.528115 156.0135 17 2.973711 58.45141 2.973711 128.6738 18 2.518891 53.62788 2.518891 110.6072 19 2.848493 79.29142 2.848493 127.6110 20 2.971362 57.81008 2.971362 129.5411 ??? [[alternative HTML version deleted]] ______________________________________________ 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.???????????????
Another approach> critical<- c(1.61,75.89, 1.61, 75.89) > colSums(sweep(mydata, 2, critical, "<"))V1 V2 V3 V4 0 19 0 0 ---------------------------------------------- David L Carlson Associate Professor of Anthropology Texas A&M University College Station, TX 77843-4352> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Jim Lemon > Sent: Monday, January 21, 2013 1:17 AM > To: Roslina Zakaria > Cc: r-help at r-project.org > Subject: Re: [R] compare and count data > > On 01/21/2013 04:39 PM, Roslina Zakaria wrote: > > Dear r-users, > > > > I have these data below: > > > > I would like to compare each column with a certain value and count > how many in each column less than that specified value. > > > > odd column (1,3,5) will compare with 1.61 and even column (2,4,6) > will compare with 75 and I would like to count how many for each > column. > > > > > > I tried these below but it give me just one set of data. > > > > critical<- c(1.61,75.89, 1.61, 75.89, 1.61, 75.89) ; critical > > pilih<- mydata[mydata< critical] ; pilih > > jumlah<- length(pilih) ; jumlah > > kuasa<- 1 - (jumlah/1000); kuasa > > > >> head(mydata,20) > > V1 V1 V1 V1 > > 1 3.014505 62.96425 3.014505 138.0673 > > 2 2.817503 56.03400 2.817503 133.3411 > > 3 2.976227 47.12192 2.976227 139.2438 > > 4 2.825495 75.05284 2.825495 129.2959 > > 5 2.793500 52.75190 2.793500 130.9874 > > 6 3.006333 54.56210 3.006333 136.2982 > > 7 3.245833 71.31358 3.245833 150.0974 > > 8 2.559980 64.28300 2.559980 114.5395 > > 9 2.548456 69.48727 2.548456 118.7383 > > 10 2.715040 58.64134 2.715040 123.0574 > > 11 2.799652 58.96281 2.799652 126.9216 > > 12 2.900839 75.43427 2.900839 131.6100 > > 13 3.183844 63.57657 3.183844 143.5530 > > 14 2.792940 63.04534 2.792940 120.1010 > > 15 3.229830 71.09790 3.229830 136.6931 > > 16 3.528115 68.64850 3.528115 156.0135 > > 17 2.973711 58.45141 2.973711 128.6738 > > 18 2.518891 53.62788 2.518891 110.6072 > > 19 2.848493 79.29142 2.848493 127.6110 > > 20 2.971362 57.81008 2.971362 129.5411 > > [[alternative HTML version deleted]] > > > > > Hi Roslina, > I think what you may want is something like this: > > colSums(t(apply(mydata,1,FUN="<",rep(c(1.61,75), > length.out=dim(mydata)[2]/2)))) > > Jim > > ______________________________________________ > 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.