When reading a csv file, where each cell can be strings or numerical values. Which approach should I use to read this csv file into a matrix. The tricky thing is that I may need to perform some computation on this imported matrix. If an entry is a string, I need to perform some character-based operation on this string, e.g., comparing it with another string. If an entry is an numerical value, I need to perform a add/subtract operation on it. How should I import this csv file:
testmatrix = as.character(read.csv("test.csv", sep=","))
testmatrix = as.vector(read.csv("test.csv", sep=","))
The data is like this
word1 word2 123 word3
234 456 word4 word5
You can read into a dataframe with one of the
read.*functions but since there are no commas,read.csvdoesn’t make much sense. Be sure to use ‘stringsAsFactors’ to avoid automatic factor creation. Convert to matrix to facilitate processing with vectorized functions likesum: