I have a table called "new" and I wanted to extract year from ManufactureDate to a new column "year".
new$ManufactureDate:
2014-01-01
2016-01-01
2005-01-01
1997-11-01
Create a new column and "new" will look like this:
ManufactureDate year
2014-01-01 2014
2016-01-01 2016
2005-01-01 2005
1997-11-01 1997
My code:
for (i in 1:nrow(new)){
new["year"] <- NA
new$year[i] <- strsplit(new$ManufactureDate, "-")[[i]][1]
print(new$year[i])
}
Result: It printed out successfully, but when I checked table "new", it shows like this, not sure what happened:
ManufactureDate year
2014-01-01 NA
2016-01-01 NA
2005-01-01 NA
1997-11-01 1997