r subscript of dataframe with condition values by a vector -


this seems rather easy, keeps busy since while.

i have dataframe (df) n columns , vector same number (n) of values.

the values in vector thresholds observations in columns in dataframe. clue is, how tell r use different thresholds each column?

i want keep observations in dataframe fulfill various thresholds each column (above or below, doesnt matter in example). observations not fulfill threshold criterion should set 0.

i dont want subset of dataframe.

can help? lot in advance.

given example data , thresholds

set.seed(42) dat <- data.frame(matrix(runif(100), ncol = 10))  ## thresholds thresh <- seq(0.5, 0.95, length.out = 10) thresh 

we can use mapply() function work out observations in each column (in this) greater or equal threshold. using indices, can replace values corresponding indices 0 via:

dat[mapply(">=", dat, thresh)] <- 0 

here call in action:

> dat           x1        x2         x3          x4         x5 1  0.9148060 0.4577418 0.90403139 0.737595618 0.37955924 2  0.9370754 0.7191123 0.13871017 0.811055141 0.43577158 3  0.2861395 0.9346722 0.98889173 0.388108283 0.03743103 4  0.8304476 0.2554288 0.94666823 0.685169729 0.97353991 5  0.6417455 0.4622928 0.08243756 0.003948339 0.43175125 6  0.5190959 0.9400145 0.51421178 0.832916080 0.95757660 7  0.7365883 0.9782264 0.39020347 0.007334147 0.88775491 8  0.1346666 0.1174874 0.90573813 0.207658973 0.63997877 9  0.6569923 0.4749971 0.44696963 0.906601408 0.97096661 10 0.7050648 0.5603327 0.83600426 0.611778643 0.61883821            x6        x7          x8         x9          x10 1  0.33342721 0.6756073 0.042988796 0.58160400 0.6674265147 2  0.34674825 0.9828172 0.140479094 0.15790521 0.0002388966 3  0.39848541 0.7595443 0.216385415 0.35902831 0.2085699569 4  0.78469278 0.5664884 0.479398564 0.64563188 0.9330341273 5  0.03893649 0.8496897 0.197410342 0.77582336 0.9256447486 6  0.74879539 0.1894739 0.719355838 0.56364684 0.7340943010 7  0.67727683 0.2712866 0.007884739 0.23370340 0.3330719834 8  0.17126433 0.8281585 0.375489965 0.08998052 0.5150633298 9  0.26108796 0.6932048 0.514407708 0.08561206 0.7439746463 10 0.51441293 0.2405447 0.001570554 0.30521837 0.6191592400 > dat[mapply(">=", dat, thresh)] <- 0 > dat           x1        x2         x3          x4         x5 1  0.0000000 0.4577418 0.00000000 0.000000000 0.37955924 2  0.0000000 0.0000000 0.13871017 0.000000000 0.43577158 3  0.2861395 0.0000000 0.00000000 0.388108283 0.03743103 4  0.0000000 0.2554288 0.00000000 0.000000000 0.00000000 5  0.0000000 0.4622928 0.08243756 0.003948339 0.43175125 6  0.0000000 0.0000000 0.51421178 0.000000000 0.00000000 7  0.0000000 0.0000000 0.39020347 0.007334147 0.00000000 8  0.1346666 0.1174874 0.00000000 0.207658973 0.63997877 9  0.0000000 0.4749971 0.44696963 0.000000000 0.00000000 10 0.0000000 0.0000000 0.00000000 0.611778643 0.61883821            x6        x7          x8         x9          x10 1  0.33342721 0.6756073 0.042988796 0.58160400 0.6674265147 2  0.34674825 0.0000000 0.140479094 0.15790521 0.0002388966 3  0.39848541 0.7595443 0.216385415 0.35902831 0.2085699569 4  0.00000000 0.5664884 0.479398564 0.64563188 0.9330341273 5  0.03893649 0.0000000 0.197410342 0.77582336 0.9256447486 6  0.74879539 0.1894739 0.719355838 0.56364684 0.7340943010 7  0.67727683 0.2712866 0.007884739 0.23370340 0.3330719834 8  0.17126433 0.0000000 0.375489965 0.08998052 0.5150633298 9  0.26108796 0.6932048 0.514407708 0.08561206 0.7439746463 10 0.51441293 0.2405447 0.001570554 0.30521837 0.6191592400 

it instructive notice mapply() returns in case:

> mapply(">=", dat, thresh)          x1    x2    x3    x4    x5    x6    x7    x8    x9   x10  [1,]  true false  true  true false false false false false false  [2,]  true  true false  true false false  true false false false  [3,] false  true  true false false false false false false false  [4,]  true false  true  true  true  true false false false false  [5,]  true false false false false false  true false false false  [6,]  true  true false  true  true false false false false false  [7,]  true  true false false  true false false false false false  [8,] false false  true false false false  true false false false  [9,]  true false false  true  true false false false false false [10,]  true  true  true false false false false false false false 

and logical values used select observations meet threshold. can different binary operator 1 used; see ?">" various options. when writing mapply() call, think of in terms of left-hand-side , right-hand-side of binary operator, such mapply() call give:

mapply(">", lhs, rhs) 

where might write

lhs > rhs 

update: @dwin has answered comment 2 thresholds update answer match.

thresh1 <- seq(0.05, 0.5, length.out = 10) thresh2 <- seq(0.55, 0.95, length.out = 10) set.seed(42) dat <- data.frame(matrix(runif(100), ncol = 10))  l1 <- mapply(">", dat, thresh1) l2 <- mapply("<", dat, thresh2) 

we can see elements match both constraints:

> l1 & l2          x1    x2    x3    x4    x5    x6    x7    x8    x9   x10  [1,] false  true false false  true  true  true false  true  true  [2,] false false false false  true  true false false false false  [3,]  true false false  true false  true  true false false false  [4,] false  true false false false false  true  true  true  true  [5,] false  true false false  true false false false  true  true  [6,]  true false  true false false  true false  true  true  true  [7,] false false  true false false  true false false false false  [8,]  true  true false  true  true false false false false  true  [9,] false  true  true false false false  true  true false  true [10,] false  true false  true  true  true false false false  true 

and same construct can used select elements match:

dat[l1 & l2] <- 0 dat  > dat           x1        x2         x3          x4         x5         x6        x7          x8 1  0.9148060 0.0000000 0.90403139 0.737595618 0.00000000 0.00000000 0.0000000 0.042988796 2  0.9370754 0.7191123 0.13871017 0.811055141 0.00000000 0.00000000 0.9828172 0.140479094 3  0.0000000 0.9346722 0.98889173 0.000000000 0.03743103 0.00000000 0.0000000 0.216385415 4  0.8304476 0.0000000 0.94666823 0.685169729 0.97353991 0.78469278 0.0000000 0.000000000 5  0.6417455 0.0000000 0.08243756 0.003948339 0.00000000 0.03893649 0.8496897 0.197410342 6  0.0000000 0.9400145 0.00000000 0.832916080 0.95757660 0.00000000 0.1894739 0.000000000 7  0.7365883 0.9782264 0.00000000 0.007334147 0.88775491 0.00000000 0.2712866 0.007884739 8  0.0000000 0.0000000 0.90573813 0.000000000 0.00000000 0.17126433 0.8281585 0.375489965 9  0.6569923 0.0000000 0.00000000 0.906601408 0.97096661 0.26108796 0.0000000 0.000000000 10 0.7050648 0.0000000 0.83600426 0.000000000 0.00000000 0.00000000 0.2405447 0.001570554            x9          x10 1  0.00000000 0.0000000000 2  0.15790521 0.0002388966 3  0.35902831 0.2085699569 4  0.00000000 0.0000000000 5  0.00000000 0.0000000000 6  0.00000000 0.0000000000 7  0.23370340 0.3330719834 8  0.08998052 0.0000000000 9  0.08561206 0.0000000000 10 0.30521837 0.0000000000 

Comments

Popular posts from this blog

jquery - Invalid Assignment Left-Hand Side -

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -