1

writing following function to add special character to an integer for example '19991221'. When I run the following function, it returns NULL value. Tried to implement Return value from function when iterating in a loop R which still returns NULL.

data is read from *.csv

fileloc=read.csv('*.csv')

m<-for (row in fileloc[1]) {
 x<-(sub("(..)$", "/\\1", row))
 y<-(sub("(.....)$", "/\\1", x))

}

> m
NULL


sample data:
  V1
1   19991221
2   19910703
3   19850326
4   19890328
5   19610912

Desired Result:
  V1
1   1999/12/21
2   1991/07/03
3   1985/03/26
4   1989/03/28
5   1961/09/12
lpt
  • 931
  • 16
  • 35
  • You are right, function `for` returns `NULL`. Which of the values of the loop do you want `m` to become? `x` or `y`? Also, are those dates? – Rui Barradas Nov 28 '17 at 10:47
  • thanks Rui, yes those are dates, if there is an easy way to convert those to dates and I do not have to use for loop, I am good with that as well, – lpt Nov 28 '17 at 10:50
  • 1
    If your dataframe is named `data`, try `gsub("-", "/", as.Date(as.character(data$V1), "%Y%m%d")) `. – Rui Barradas Nov 28 '17 at 10:53
  • Thank, you, it worked! – lpt Nov 28 '17 at 11:00

0 Answers0