Adding Multiple Data Frame
Title
Question
Firstly, I performed rbind to add new data row.
Then, the csv file is updated by doing write.csv operation.
By running the R script file again, new column (X) exists in the dataframe and that makes number of columns of arguments do not match problem for further data frame addition.
How to solve it?
I also attached my code.
captaincy=read.csv("CaptaincyData.csv")
captaincy <- rbind(captaincy,data.frame(names="Dravid",Y=2008,played=25,won=8,lost=6,victory=0.32))
write.csv(captaincy,"CaptaincyData.csv")
In adding new column using cbind by running R script file repeatedly, new column exists. (in this case: just only new column exists)
R Operations-on-Matrices-and-Data-Frames 08-09 min 40-50 sec
Answers:
Please send us the exact error which you are encountering.
Error in rbind(deparse.level, ...) :
numbers of columns of arguments do not match
Please view the captaincy data set by using the "View(captaincy)" command and check whether there are any other columns except names, Y, played, won, lost and victory. If there are, then that is creating the problem of mismatch.
I encounters the error ( rbind error) after running the following R file repeatedly.
"matrix.R" file
captaincy=read.csv("CaptaincyData.csv")
captaincy <- rbind(captaincy,data.frame(names="Dravid",Y=2008,played=25,won=8,lost=6,victory=0.32))
write.csv(captaincy,"CaptaincyData.csv")
And new column exists automatically by running the above-mentioned code repeatedly.
So, the question is why that column exists automatically by running the same code repeatedly
Login to add comment
I encounters the error ( rbind error) after running the following R file repeatedly.
"matrix.R" file
captaincy=read.csv("CaptaincyData.csv")
captaincy <- rbind(captaincy,data.frame(names="Dravid",Y=2008,played=25,won=8,lost=6,victory=0.32))
write.csv(captaincy,"CaptaincyData.csv")
And new column exists automatically by running the above-mentioned code repeatedly.
So, the question is why that column exists automatically by running the same code repeatedly
When you are creating a CSV file then it is automatically assuming the index column of the data frame as a separate data column and hence adding it separately in the CSV file which explains why you are getting more columns every time you run the command. This is because you are accessing the newly created "CaptaincyData.csv" file which has one more column than the original one.
There may be cases where the existing csv file needs to be modified by adding new data frames frequently.
In that case, how to handle?
The <a href="https://r-lang.com/cbind-in-r/">cbind()</a> and <a href="https://r-lang.com/rbind-in-r/" target="" title="">rbind()</a> functions are generic methods for data frames. These data frame functions will be used if at least one argument is a data frame and the other arguments are vectors or matrices. To merge two data frames (datasets) horizontally, use the merge() function in the R language.
Login to add comment