Transform or convert the column of a data frame in R -


in special case want convert data in data frame column boolean/logical values depending on condition. think question used every converstion/transformation own data frame column?

example:

> sleep    group id 1    0.7     1  1 2   -1.6     1  2 3   -0.2     1  3 4   -1.2     1  4 5   -0.1     1  5 6    3.4     1  6 7    3.7     1  7 8    0.8     1  8 9    0.0     1  9 10   2.0     1 10 11   1.9     2  1 12   0.8     2  2 13   1.1     2  3 14   0.1     2  4 15  -0.1     2  5 16   4.4     2  6 17   5.5     2  7 18   1.6     2  8 19   4.6     2  9 20   3.4     2 10 > l = sleep$extra < 0 > l  [1] false  true  true  true  true false false false false false false false [13] false false  true false false false false false 

i want l column inside data frame (or new one).

this add new column named l , assign values (sleep$extra < 0) it.

sleep["l"] <- sleep$extra < 0   #   group id     l #1    0.7     1  1 false #2   -1.6     1  2  true #3   -0.2     1  3  true #4   -1.2     1  4  true #5   -0.1     1  5  true #6    3.4     1  6 false #7    3.7     1  7 false #8    0.8     1  8 false #9    0.0     1  9 false #10   2.0     1 10 false #11   1.9     2  1 false #12   0.8     2  2 false #13   1.1     2  3 false #14   0.1     2  4 false #15  -0.1     2  5  true #16   4.4     2  6 false #17   5.5     2  7 false #18   1.6     2  8 false #19   4.6     2  9 false #20   3.4     2 10 false 

or can cbind

cbind(sleep, l) 

which add new column (l) sleep dataframe.

if want replace same column (extra) boolean/ logical values can assign values same variable.

sleep$extra <- sleep$extra < 0   #   group id #1  false     1  1 #2   true     1  2 #3   true     1  3 #4   true     1  4 #5   true     1  5 #6  false     1  6 #7  false     1  7 #8  false     1  8 #9  false     1  9 #10 false     1 10 #11 false     2  1 #12 false     2  2 #13 false     2  3 #14 false     2  4 #15  true     2  5 #16 false     2  6 #17 false     2  7 #18 false     2  8 #19 false     2  9 #20 false     2 10 

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -