Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert a vector into a specific cell of data frame in R

Tags:

r

I have a small situation in R.

I have a dataframe as below:

                 age
numCategories      9
signFeatures      NA
nullDeviance      NA
residualDeviance  NA
aic               NA

I want to insert a vector as c(1,2,3) in a particular cell of the data frame.

For example my data frame after replacement should look somethinglike:

                  age
numCategories      9
signFeatures      c(1,2,3)
nullDeviance      NA
residualDeviance  NA
aic               NA

I tried doing the below:

df['signFeatures', 'age'] <- c(1,2,3) &
df['signFeatures', 'age'] <- Vectorize(c(1,2,3))

Both the time it gives me the same error:

Error in `[<-.data.frame`(`*tmp*`, "signFeatures", "age", value = c(1,  : 

replacement has 3 rows, data has 1

I understand the problem, but cant find a way to solve it.

Any help would be appreciated

like image 949
Sam Avatar asked Oct 14 '25 15:10

Sam


1 Answers

Like Andrew mentioned above, each column of a data frame has to have to same class, so you can record, instead, a transposed version of your desired table above:

data <- data.frame(numCategories = 9)
data$signFeatures <- list(c(1,2,3))
data$nullDeviance <- rnorm(1)
data$residualDeviance <- rnorm(1)
data$aic <- rnorm(1)
like image 74
swtlk Avatar answered Oct 17 '25 07:10

swtlk