Unable to understand why a loop doesn't work

Hi,
I'm looking for solution I don't manage to understand why the following loop doesn't work :

tovector <- function(a) {
for(i in 1:(dim(a)[1])) {
b<-a[i,3]
b<-as.character(b)
b<-as.vector(el(strsplit(b,"\r\n")))
a[i,3]<-b
#a[i,11]<-as.vector(el(strsplit(as.character(a[i,11]),"\r\n")))
#a[i,14]<-as.vector(el(strsplit(as.character(a[i,14]),"\r\n")))
}
}

The error I get when I try to run this function is the following :

tovector(sondage)
Erreur : Assigned data b must be compatible with row subscript i.
x 1 row must be assigned.
x Assigned data has 5 rows.
i Row updates require a list value. Do you need list() or as.list()?
Run rlang::last_error() to see where the error occurred.

The aim of this function is to convert some columns of a table into vector of strings.
I would then put each element of each vector in a different colum.

The issue I have is that I don't understand why the value stocked in b cannot replace the content of the cell.

Thanks for reading me !

I think you have to return a as the result of the function.
When I apply the function on a simple case of sondage I get no errors. See below.
Therefore show us your input (or a simplified version of that): the most simple case that still gives the error.

tovector <- function(a) {
for(i in 1:(dim(a)[1])) {
b<-a[i,3]
b<-as.character(b)
b<-as.vector(el(strsplit(b,"\r\n")))
a[i,3]<-b
#a[i,11]<-as.vector(el(strsplit(as.character(a[i,11]),"\r\n")))
#a[i,14]<-as.vector(el(strsplit(as.character(a[i,14]),"\r\n")))
}
a
}

sondage <- matrix(data=1:18,ncol=6,byrow=T)
print(sondage)
#>      [,1] [,2] [,3] [,4] [,5] [,6]
#> [1,]    1    2    3    4    5    6
#> [2,]    7    8    9   10   11   12
#> [3,]   13   14   15   16   17   18
tovector(sondage)
#>      [,1] [,2] [,3] [,4] [,5] [,6]
#> [1,] "1"  "2"  "3"  "4"  "5"  "6" 
#> [2,] "7"  "8"  "9"  "10" "11" "12"
#> [3,] "13" "14" "15" "16" "17" "18"
Created on 2022-05-10 by the reprex package (v2.0.1)

Hi thanks for your swift answer, I believe I have identify the origin of the error, but I'm not sure I understand it fully:

class(sondage[1,3])
[1] "tbl_df" "tbl" "data.frame"

sondage[1,3]

A tibble: 1 x 1

Dans quels formats jouez-vous ?...3

1 "DC\r\nLegacy\r\nModern\r\nPioneer\r\nLimité"

To give a bit of context, my data come from an excel sheet that I read with the library read_xlsx.
It is a data from a form of an association that organize events and want to know the opinion of its community.

The columns presenting problems (the 3rd, the 11th and the 14th) contain the answers of multiple choice questions and thos answers seems to be table or lists elements in r.
(As I understand it.)

Yet I do not understand why it doesn't work.
Is there things that we cannot do given some class presents in a column ?

tibbles dont unwrap automatically unlike data.frames, they need you to specifically unwrap them to achieve that effect.


(a1 <- data.frame(a=1))
(b2 <- tibble(b=2))

a1[1,1]
b2[1,1]

identical(a1[[1,1]], a1[1,1])
identical(b2[[1,1]], b2[1,1])

b2[[1,1]]

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.