简体   繁体   中英

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.

Could you please try following:(I am putting data in here you could use 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 ):

  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:

  1. The first column is a factor so you can only add levels that that factor has.
  2. If c(...) is used with a mix of characters and numerics the result is character.
  3. data is a data.frame, not a matrix -- the question incorrectly states it is a matrix. (If it were a matrix then it would be a character matrix since you can't mix character and numeric in a matrix.)

To fix these problems use as.is = TRUE in read.csv and use list instead of 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

Note

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)

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.

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(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

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM