I would like to use the tiyverse separate_rows
function to separate rows where >1 numerical values are in the same row. I can do it on a dataframe, but not a list of dataframes, and I don't understand why. All dataframes in the list have the same column names, just different number of rows.
a<-c("a","b","c")
b<-c(1,"1,2",3)
df1<-data.frame(a,b)
df2<-df1
df2[2,2]="1"
I can use separate_rows
on df1
separate_rows(df1,b)
> separate_rows(df1,b)
a b
1 a 1
2 b 1
3 b 2
4 c 3
But not on a list:
mylist<-list(df1,df2)
lapply(mylist, function(x) separate_rows(b))
Because I get an error
> lapply(mylist, function(x) separate_rows(b))
Error in UseMethod("separate_rows_") :
no applicable method for 'separate_rows_' applied to an object of class "character"
Called from: separate_rows_(data, cols = cols, sep = sep)
What am I doing wrong?
EDIT: I know what I was doing wrong (not passing all the arguments)
You need to pass the df as well
lapply(mylist, function(x) separate_rows(x,b))
Here is a simple non-elegant solution but it works
lapply(mylist, function(x) {y <- separate_rows(x,b)
y['b'] <- as.numeric(y[['b']])
y}) %>% str()
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.