简体   繁体   中英

How to print the function output using for loop

I am trying to print the normalized value for all three columns separately.For this I am using the for loop, instead I get only the last column with the print function,while no column without a print function.How can I get all the outputs separately??

library(tibble)
data_frame<-tibble(c1=rnorm(50,4,2),
               c2=rnorm(50,4,2),
               c3=rnorm(50,4,2))


   normalize<-function(l)
    {
       (l-min(l))/(max(l)-min(l))
    }
       for (i in length(data_frame))
   {
 normalize(data_frame[i])
   }

Try mutate_all

library(dplyr)
normalize <- function(x) (x - min(x)) / (max(x) - min(x))
data_frame %>% mutate_all( normalize )

or

replace(data_frame, TRUE, lapply(data_frame, normalize))

or if you don't mind overwriting data_frame:

data_frame[] <- lapply(data_frame, normalize)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM