简体   繁体   中英

R: manipulate dataframe cell using named numeric vector

I want to (manually) overwrite cells in column x of the dataframe df . But R produces an error. Consider

m = 1:2
n = 3:4
names(m)= c("o", "we")
names(n)= c("bn","lt")
s = c( "bb", "cc") 
b = c( FALSE, TRUE)
df = data.frame( s, b)
df$x= list(m,n)

Now replace column x first row:

k = 5:6
names(k)= c("jh","jh")
df[1,"x"] = k  ## error occurs here

Try this: df$x[[1]] = k

Since you are using a data frame you can not speak of columns and rows.. so you have to access the elements of the data frame with $ instead of "x" .

Further, you are using lists in your data frame. You should access the elements of a list with [[1]] or any other position of the element in the list.

Hope this helps.

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