i've read this and have created a lapply function to add a "SubCat" column to every data frame within a list.
This is my code:
my_list <- lapply(1:length(my_list),
function(i) cbind(my_list[[i]], my_list[[i]]["SubCat"] <- as.character("")))
But get this error:
Error in `[<-.data.frame`(`*tmp*`, "SubCat", value = "") :
replacement has 1 row, data has 0
Whats wrong?
When i use it, on a single data frame it works:
my_list[[1]]["SubCat"] <- as.character("")
UPDATE:
These are examples of my data frames, they all contain the same structure. One column for SKU and one for the Category.
DataFrame 1:
row.names SKU Tv.y.Video
1 1699 2018143169254P Tv.y.Video
2 1700 2018143169254 Tv.y.Video
3 1946 2018144678120P Tv.y.Video
4 1947 2018144678120 Tv.y.Video
5 2366 2018146411831P Tv.y.Video
6 2367 2018146411831 Tv.y.Video
DataFrame 2:
row.names SKU C�mputo
1 6 2004121460000P C�mputo
2 7 2004121460000 C�mputo
3 8 2004121440002P C�mputo
4 9 2004121440002 C�mputo
5 10 2004123030003P C�mputo
6 11 2004123030003 C�mputo
When i applied my code to just one df it works:
my_list[[1]]["SubCat"] <- as.character("")
Result:
row.names SKU Tv.y.Video SubCat
1 1699 2018143169254P Tv.y.Video
2 1700 2018143169254 Tv.y.Video
3 1946 2018144678120P Tv.y.Video
4 1947 2018144678120 Tv.y.Video
5 2366 2018146411831P Tv.y.Video
6 2367 2018146411831 Tv.y.Video
UPDATE 1:
I also have some empty data.frames in the list.