Error: x must be atomic for 'sort.list'
From the output of str(cc2)
, the variable inside of the data.table, V1, is itself a list. This means that cc2 is a nested list of length 1. The error is occurring because table
calls sort.list
, which requires an atomic vector as input.
Try using unlist
:
cc3 <- as.data.frame(table(unlist(cc2)))
unlist
will (recursively) extract elements from their list containers. So unlist(cc2)
will return a vector, which works directly with table
.
R: Error in sort.list(bx[m$xi]) : 'x' must be atomic for 'sort.list' Have you called 'sort' on a list? Merge data frames error
As far as I can see, the problem is that you have a nested dataframe. I can't think of any solution except unlisting the nested dataframe (converting it to a vector):
tk1_h$profile <- unlist(tk1_h$profile)
Error in sort.list(y) : 'x' must be atomic for 'sort.list' Have you called 'sort' on a list?
Converting tibble
to data.frame
should help.
library(dummies)
library(dplyr)
combi %>%
data.frame() %>%
dummy.data.frame(names = c('Outlet_Size', 'Outlet_Location_Type', 'Outlet_Type', 'Item_Type_New'), sep='_')
Error can be reproduced using below code:
library(dummies)
library(dplyr)
library(tibble)
iris %>%
as.tibble() %>%
#data.frame() %>%
dummy.data.frame(names = c('Species'), sep='_')
#Error in sort.list(y) : 'x' must be atomic for 'sort.list'
#Have you called 'sort' on a list?
Here if you uncomment data.frame()
step this error will be gone.
dummification throws an error.'x' must be atomic for 'sort.list'
Here's two solution using the dummies
package. I can't see from your question if the dummy
calls is from the dummies
package. Regardless,
first some data,
categoricalVar <- data.frame(
FavouriteSource = c('bar', 'foo', 'foo', 'foobar', 'foo', 'foo'),
FavouriteSource30 = c('A', 'C', 'C', 'B', 'B', 'A')); categoricalVar
#> FavouriteSource FavouriteSource30
#> 1 bar A
#> 2 foo C
#> 3 foo C
#> 4 foobar B
#> 5 foo B
#> 6 foo A
then load the dummies
library
# install.packages(c("dummies"), dependencies = TRUE)
library(dummies)
and here the dummy.data.frame()
method to get dummies,
dummy.data.frame(categoricalVar)
#> FavouriteSourcebar FavouriteSourcefoo FavouriteSourcefoobar FavouriteSource30A
#> 1 1 0 0 1
#> 2 0 1 0 0
#> 3 0 1 0 0
#> 4 0 0 1 0
#> 5 0 1 0 0
#> 6 0 1 0 1
#> FavouriteSource30B FavouriteSource30C
#> 1 0 0
#> 2 0 1
#> 3 0 1
#> 4 1 0
#> 5 1 0
#> 6 0 0
or as Sathish suggest in the comment above,
lapply(categoricalVar, dummy)
#> $FavouriteSource
#> categoricalVarbar categoricalVarfoo categoricalVarfoobar
#> [1,] 1 0 0
#> [2,] 0 1 0
#> [3,] 0 1 0
#> [4,] 0 0 1
#> [5,] 0 1 0
#> [6,] 0 1 0
#>
#> $FavouriteSource30
#> categoricalVarA categoricalVarB categoricalVarC
#> [1,] 1 0 0
#> [2,] 0 0 1
#> [3,] 0 0 1
#> [4,] 0 1 0
#> [5,] 0 1 0
#> [6,] 1 0 0
R - merge two dataframes - Error in sort.list(bx[m$xi]) : 'x' must be atomic for 'sort.list'
df2$key_data
is of class different to that of df1
#df2$key_date <- as.Date(df2$key_date, "%Y-%M-%d")
df2$key_date <- as.POSIXct(df2$key_date)
mdf <- merge(x = df1, y = df2, by = "key_date", all.x = TRUE)
EDIT:
There is a difference in dates in "AEST" and "GMT" with the times in two df's.
df2$key_date <- as.POSIXct(df2$key_date)
df2$key_date
# [1] "2017-02-04 AEST" "2017-02-03 AEST"
df1$key_date
#[1] "2017-02-03 GMT" "2017-02-03 GMT" "2017-02-03 GMT" "2017-02-03 GMT"
#[5] "2017-02-03 GMT" "2017-02-03 GMT"
df2$key_date <- as.character(df2$key_date)
df1$key_date <- as.character(df1$key_date)
merge(x = df1, y = df2, by = "key_date", all.x = TRUE)
# key_date date NO2 PM10_1 PM10_2 PM10_3 site code
# 1 2017-02-03 2017-02-03 10:00:00 7.4 18.3 NA NA anon1 anon1
# 2 2017-02-03 2017-02-03 10:15:00 6.7 NA 27.5 NA anon1 anon1
# 3 2017-02-03 2017-02-03 10:30:00 7.0 NA 27.9 NA anon1 anon1
# 4 2017-02-03 2017-02-03 10:45:00 6.1 NA 28.9 NA anon1 anon1
# 5 2017-02-03 2017-02-03 11:00:00 5.8 21.1 28.2 NA anon1 anon1
# 6 2017-02-03 2017-02-03 11:15:00 8.8 NA 28.1 NA anon1 anon1
# airport ws wd tempi humidity
# 1 LCY 13 185 8 78
# 2 LCY 13 185 8 78
# 3 LCY 13 185 8 78
# 4 LCY 13 185 8 78
# 5 LCY 13 185 8 78
# 6 LCY 13 185 8 78
Categorical Bar Graph 'x' must be atomic for 'sort.list'
Assume df -- data frame and "a" and "b are columns.
Try below codes--and image of plot attached.ggplot(data = df,aes(x=df$a,fill=df$b))+geom_bar()
Error in sort.list(y) : 'x' must be atomic for 'sort.list'
I guess last command in your code should look like this:
lasso <- lars(x=as.matrix(training), y=training$CompressiveStrength, type="lasso", trace=FALSE,normalize = TRUE)
Accessing column with df[col] gives: Error 'x' must be atomic for 'sort.list'
The function [
applied to a data frame returns a data frame (if only one argument is used). If you want to access a single column and return it as a vector, you have to use [[
instead.
table[1] <- factor(table[[1]])
But this may not be necessary since both columns are factors (see your str
output).
By the way: table
is not a good name for an object, since it's also the name of a basic R function.
Related Topics
Plotting Pie Charts in Ggplot2
How to Plot X-Axis Labels and Bars Between Tick Marks in Ggplot2 Bar Plot
Flatten Nested Lists in a List
Adjusting the Node Size in Igraph Using a Matrix
Changing the Appearance of Facet Labels Size
Create Fillable PDF Textbox via R
How to Apply Separate Coord_Cartesian() to "Zoom In" into Individual Panels of a Facet_Grid()
Prevent Knitr/Rmarkdown from Interleaving Chunk Output with Code
How to Reference Column Names That Start with a Number, in Data.Table
Format Axis Tick Labels to Percentage in Plotly
Error: Object '.Dosnowglobals' Not Found
How to Determine If a Character Vector Is a Valid Numeric or Integer Vector
Can Transparency Be Used with Postscript/Eps
Stargazer Left Align Latex Table Columns
Get(X) Does Not Work in R Data.Table When X Is Also a Column in the Data Table
Curly Curly Tidy Evaluation and Modifying Inputs or Their Names