Paul85 Paul85 - 3 months ago 9
R Question

R number of items to replace is not a multiple of replacement length for creating list

Hi I am trying to create a list where each index will store different data based on matching condition.I have following data:

col1 col2 col3
500x high 9.07
5x low 23.6
50x medium 11.64
500x high 8.24
5x low 35.17
50x medium 12.82


I have following R script:

somelist<-list()
tempinfo<-("500x","50x","5x")
for(k in seq(from=1,to=nrow(dataset),by=3)){
for(y in 0:2){
if (any(as.character(dataset[,1][k+y]) == tempinfo)) {
somelist[as.character(dataset[,1][k+y])]<-append(somelist[as.character(dataset[,1][k+y])],as.character(dataset[,3][k+y]))
}
}
}


It should be give output like:

somelist
$500x
[1] 9.07 8.24
$50x
[1] 11.64 12.82
$5x
[1] 23.6 35.17


But unfortunately I am getting following error:
number of items to replace is not a multiple of replacement length

Anybody knows why?
Thanks

42- 42-
Answer

Basically a ratification of comments. (I tried finding a duplicate ith a couple of different searches and failed, although I am pretty sure there must be one.)

> dat <- read.table(text="col1    col2    col3
+ 500x    high    9.07
+ 5x      low     23.6
+ 50x     medium  11.64
+ 500x    high    8.24
+ 5x      low     35.17
+ 50x     medium  12.82", head=T)
> split(dat$col3, dat$col1)
$`500x`
[1] 9.07 8.24

$`50x`
[1] 11.64 12.82

$`5x`
[1] 23.60 35.17