Adding character in matrix in R
Clash Royale CLAN TAG#URR8PPP
Adding character in matrix in R
I import csv by.
data<-read.csv('test.csv')
and then data has value
> data
name a b c d
1 a 1 2 1 1
2 b 2 2 2 4
now i wish to add row at the bottom of data so,I add
results <- c('aa',2,3,4,5)
data[nrow(data)+1,]<-results
But it cause me warring and character value is not inserted.
Warning message:
In `[<-.factor`(`*tmp*`, iseq, value = "aa") :
invalid factor level, NA generated
and data has value =
> data
name a b c d
1 a 1 2 1 1
2 b 2 2 2 4
3 <NA> 2 3 4 5
but i want to have
> data
name a b c d
1 a 1 2 1 1
2 b 2 2 2 4
3 aa 2 3 4 5
I am new in R and have tried for the ans for many hours so any help will be appreciated.Thank you.
dt = data.frame(x=c("A","B"), y=1:2, stringsAsFactors = T); dt[3,] = c("C",3); dt = data.frame(x=c("A","B"), y=1:2, stringsAsFactors = F); dt[3,] = c("C",3)
4 Answers
4
This should work.
data <- read.csv('test.csv', stringsAsFactors = FALSE)
In a short explanation, read.csv treat every character column as a factor by default. Factor are a special class of data that can only take a few values determined at its creation.
Could you please try following:(I am putting data in here you could use read.csv
)
read.csv
dat <- read.table(text = "name a b c d
1 a 1 2 1 1
2 b 2 2 2 4",
header = TRUE,stringsAsFactors = FALSE)
dat
results <- c('aa',2,3,4,5)
rbind(dat,results)
Output(without using stringAsFactors = FALSE
):
stringAsFactors = FALSE
name a b c d
1 a 1 2 1 1
2 b 2 2 2 4
3 <NA> 2 3 4 5
Warning message:
In `[<-.factor`(`*tmp*`, ri, value = "aa") :
invalid factor level, NA generated
Output without warning message:
name a b c d
1 a 1 2 1 1
2 b 2 2 2 4
3 aa 2 3 4 5
There are several problems here:
c(...)
data
To fix these problems use as.is = TRUE
in read.csv
and use list
instead of c
:
as.is = TRUE
read.csv
list
c
data <- read.csv("test.csv", as.is = TRUE)
data[nrow(data) + 1, ] <- list('aa',2,3,4,5)
giving:
> data
name a b c d
1 a 1 2 1 1
2 b 2 2 2 4
3 aa 2 3 4 5
The file in reproducible form is generated from:
Lines <- "name a b c d
1 a 1 2 1 1
2 b 2 2 2 4
"
temp <- read.table(text = Lines, as.is = TRUE)
write.csv(temp, "test.csv", row.names = FALSE, quote = FALSE)
Include 'aa' as one of the levels
and it should work with the OP's factor
column. Also, as list
can hold different type
instead of a vector (formed with c
), assign the next row with a list
as input
levels
factor
list
type
c
list
levels(data$name) <- c(levels(data$name), 'aa')
data[nrow(data) +1,] <- list('aa', 2, 3, 4, 5)
data
# name a b c d
#1 a 1 2 1 1
#2 b 2 2 2 4
#3 aa 2 3 4 5
data <- structure(list(name = structure(1:2, .Label = c("a", "b"),
class = "factor"),
a = 1:2, b = c(2L, 2L), c = 1:2, d = c(1L, 4L)), .Names = c("name",
"a", "b", "c", "d"), row.names = c("1", "2"), class = "data.frame")
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Try not to have factor variables in your data frame and your code will work. This simple example will show the difference
dt = data.frame(x=c("A","B"), y=1:2, stringsAsFactors = T); dt[3,] = c("C",3); dt = data.frame(x=c("A","B"), y=1:2, stringsAsFactors = F); dt[3,] = c("C",3)
– AntoniosK
Aug 12 at 13:15