0

I have got a large list (20 elements, 4.2Mb) called Buffer_list, which I converted into a data frame (now Buffer_norm) like this:

#changing the List to a data frame with max for the biggest element
max.length <- max(sapply(Buffer_list, length))
List_norm <- lapply(Buffer_list, function(v) { c(v, rep(NA, max.length-length(v)))})
Buffer_norm <- as.data.frame(List_norm)
names(Buffer_norm) = c(1:20)

Every column is now of the same length (57.443 rows) with NAs for the shorter lists.

When viewing the data.frame, it shows me, that all values are numeric. The same when I try this:

sapply(Buffer_norm, mode)
        1         2         3         4         5         6         7         8         9        10        11        12        13 
"numeric" "numeric" "numeric" "numeric" "numeric" "numeric" "numeric" "numeric" "numeric" "numeric" "numeric" "numeric" "numeric" 
       14        15        16        17        18        19        20 
"numeric" "numeric" "numeric" "numeric" "numeric" "numeric" "numeric" 
> sapply(Buffer_norm, class)
        1         2         3         4         5         6         7         8         9        10        11        12        13 
"numeric" "numeric" "numeric" "numeric" "numeric" "numeric" "numeric" "numeric" "numeric" "numeric" "numeric" "numeric" "numeric" 
       14        15        16        17        18        19        20 
"numeric" "numeric" "numeric" "numeric" "numeric" "numeric" "numeric" 

Whereas it shows me, that the values are not numeric, when

plot(density(Buffer_norm))

Error in density.default(Buffer_norm) : argument 'x' must be numeric I would like to plot a density histogram of the data.frame for every column itself with a y axis from 0 to 1. The added NAs from converting the list to a data.frame have to be kept in mind!

I tried to follow this: R Normalize then plot two histograms together in R and ggplot2 histogram with density curve that sums to 1 without any success!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Lisa
  • 47
  • 8

1 Answers1

0

Your Buffer_norm is a dataset, you must select one variable. Please check this example with mtcars data.

str(mtcars)
plot(density(mtcars))
 Error in density.default(mtcars) : argument 'x' must be numeric
plot(density(mtcars[, 1]))

enter image description here

paoloeusebi
  • 1,056
  • 8
  • 19
  • `> plot(density(Buffer_norm[,1])) Error in density.default(Buffer_norm[, 1]) : 'x' contains missing values` it does not work either – Lisa Nov 28 '18 at 11:03
  • plot(density(Buffer_norm[,1])) Error in density.default(!is.na(Buffer_norm[, 1])) – paoloeusebi Nov 28 '18 at 11:04
  • if I use `!is.na` the error x has to be numeric occurs and if I use `na.rm=TRUE` "Error in density.default(Buffer_norm[, 1]) : 'x' contains missing values". So acutally both is not working.... – Lisa Nov 28 '18 at 14:02
  • Would you like to put here a link to the data? – paoloeusebi Nov 28 '18 at 14:51
  • By not just selecting the right colomn, but also the rows until the NAs start, it works now! like this: `plot(density(Buffer_DF[1:26208,1]))` – Lisa Dec 06 '18 at 09:59