0

I have 2142 rows and 9 columns in my data frame. When I call head(df), the data frame appears fine, something like below:

Local Identifier Local System Parent ID Storage Type Capacity Movable? Storage Unit Order Number
2209       NEZ0037-76 FreezerWorks   NEZ0037       BoxPos        1        N                        76
2210       NEZ0037-77 FreezerWorks   NEZ0037       BoxPos        1        N                        77
2211       NEZ0037-78 FreezerWorks   NEZ0037       BoxPos        1        N                        78
2212       NEZ0037-79 FreezerWorks   NEZ0037       BoxPos        1        N                        79
2213       NEZ0037-80 FreezerWorks   NEZ0037       BoxPos        1        N                        80
2214       NEZ0037-81 FreezerWorks   NEZ0037       BoxPos        1        N                        81
     Description Storage.Label
2209                        I4
2210                        I5
2211                        I6
2212                        I7
2213                        I8
2214                        I9`

However, when I call write.csv or write.table, I get an incoherent output. Something like below:

Local Identifier    Local System    Parent ID   Storage Type    Capacity    Movable 
1   NEZ0011 FreezerWorks    NEZ0011 Box-9X9 81  Y
39  40  41  42  43  44  45
80  81   "Box-9X9 NEZ0014"  1   2   3   4
38  39  40  41  42  43  44
79  80  81   "Box-9X9 NEZ0017"  1   2   3
37  38  39  40  41  42  43
78  79  80  81   "Box-9X9 NEZ0020"  1   2
36  37  38  39  40  41  42
77  78  79  80  81   "Box-9X9 NEZ0023"  1
35  36  37  38  39  40  41
76  77  78  79  80  81   "Box-9X9 NEZ0026"`

Calling sapply(df, class) reveals that all columns in the data frame are [1] "factor" except for $Storage.Level which is [1] "data.table" "data.frame". When I called unlist on $Storage.Level, the output is better but it changes the value in the column. I also tried df <- data.frame(df, stringsAsFactors=FALSE) without success. Also data.frame(lapply(df, factor)) as suggested in the thread here and as.data.frame in the thread here did not work. Is there a way to unlist $Storage.Level without tampering with the values in the column? Or maybe there is a way to change from level "data.table" "data.frame" to factor and output the data safely.

R version 3.0.3 (2014-03-06)

Community
  • 1
  • 1
sedeh
  • 7,083
  • 6
  • 48
  • 65
  • 1
    Can you format your output properly, please? This is highly unreadable and hard to spot what's going wrong. – Roman Luštrik Jul 03 '14 at 11:30
  • 1
    You seem to have a data table inside a data frame (that is the variable of class `"data.table" "data.frame"` that you say is `Storage.Level`, but appears as `Storage.Label` in the output). This could well be causing it. Can you try temporarily removing this variable and then saving the rest of the data? – konvas Jul 03 '14 at 12:29

1 Answers1

1

It sounds like you have something like this:

df <- data.frame(A = 1:2, C = 3:4)
df$AC <- data.table(df)
str(df)
# 'data.frame':  2 obs. of  3 variables:
#  $ A : int  1 2
#  $ C : int  3 4
#  $ AC:Classes ‘data.table’ and 'data.frame':  2 obs. of  2 variables:
#   ..$ A: int  1 2
#   ..$ C: int  3 4
#   ..- attr(*, ".internal.selfref")=<externalptr> 
sapply(df, class)
# $A
# [1] "integer"
# 
# $C
# [1] "integer"
# 
# $AC
# [1] "data.table" "data.frame"

If that's the case, you will have trouble writing to a csv file.

Try first calling do.call(data.frame, your_data_frame) to see if that sufficiently "flattens" your data.frame, as it does with this example.

str(do.call(data.frame, df))
# 'data.frame':  2 obs. of  4 variables:
#  $ A   : int  1 2
#  $ C   : int  3 4
#  $ AC.A: int  1 2
#  $ AC.C: int  3 4

You should be able to write this to a csv file without any problems.

A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485