I have 10 datasets in a folder, with 4 columns, which I wish to read in as seperate dataframes in r, for which I use the following to do:
temp = list.files(pattern="*.csv")
for(i in 1:length(temp)){
assign(paste("name",i,sep = ""), as.data.frame(read.table(temp[i])))
}
Then if i want to change the column names as well as adding a new column V5 <- V3**2 in either the same loop or a different loop, how could this be done.
The other suggestions for changing column names i've seen here in stackoverflow suggest creating a list of columns and then changing them. But they dont change the data in the global environment.
Could any of you help with this?
Many thanks.
assignin almost every situation. In this case, I'd suggest the data be in a list, alax <- lapply(temp, read.table). If you need to add columns, you can dox <- lapply(x, function(L) transform(L, V5=V3^2)).colnames(x[[3]]) <- c(...). If you want to change the second column name in all of them, thenx <- lapply(x, function(L) { colnames(L)[2] <- "quux"; L; }).