Data Manipulation in R: 'X' must be atomic

后端 未结 2 1781
北恋
北恋 2021-02-15 16:49

I have imported a file with headings and numbers in multiple columns using the following command. irs_data <- read.csv(file=\"10incyallnoagi.csv\")

I wou

相关标签:
2条回答
  • 2021-02-15 17:19

    you can use the unlist() to convert the list to a vector as the sort() function takes vector form for sorting. so just use

    head(sort(unlist(new_var), decreasing=TRUE), 3) 
    
    0 讨论(0)
  • 2021-02-15 17:29

    The problem is that salary_var is a list containing a single-element. The call to sort() is then trying to sort a list, not an atomic element. You can see that salary_var is a list by running str(salary_var). If you omit the c(), you'll instead end up with a data frame with a single column, which gives the same problem.

    Two simple solutions:

    To sort the values in the element of the list, use

    head(sort(salary_var[[1]], decreasing=TRUE), 3) 
    

    where the [[1]] selects the first element of the list and sorts the values within it.

    Alternatively, create salary_var explicitly as a numeric vector instead:

    salary_var <- (irs_data[13]/irs_data[12])[[1]]
    

    One note: in your post, you wrote new_var instead of salary_var in your call to sort() which may confuse other readers.

    0 讨论(0)
提交回复
热议问题