Get column from table

rstudio
datatable

#1

Hi guys,

we have a data table as a txt file we are importing in RStudio.
The data table has multiple columns as the one called reactionTime which has countless lines of float values.
We are desparately trying to save the column as a list or vector into a variable but it always gets imported as an integer.

We can't use subset() as we want the whole column to be imported.

When we combine two variables, correctOnes (a subset of all lines of the column accuracy that have the value 1) and reactionTime using correctOnes$reactionTime we get NULL and when trying to barplot them we get the 'height must be a vector or matrix' error.

So the struggle is how to accurately import the line reactionTime from the data table without using barplot().

Any help is appreciated, thanks in advance!
Malte & Vanessa


#2

Does this work?

library('tidyverse')
d = read_table(file = 'path/to/my/file.txt')
my_vec = d %>% pull(reactionTime)

#3

Are you sure you mean read_table, not read.table? I can import the data table using the latter - using the first one after having to eliminate the header statement I get the following:

> my_vec = d %>% pull(reactionTime)
Error: `var` must evaluate to a single number or a column name, not an integer vector

Using read.table leads in correctOnes$my_vec to NULL and in barplot(sort(correctOnes$my_vec)) to Error in barplot.default(sort(correctOnes$my_vec)) : 'height' must be a vector or matrix In addition: Warning message: In is.na(x) : is.na() applied to non-(list or vector) of type 'NULL'

Thanks so far!


#4

Hello,

Could you please post more information on your data structure or a sample of the data?


#5

Alright. Please have a look at the header plus the first three lines of our data:

participant gender age name stimulusNr stimulusFinalSound stimulusSyllableCount response reactionTime accuracy
1 W 20 Leonie 27 V 2 right 4.658.930.309.931.340 1
1 W 20 Leonie 48 K 2 left 8.419.140.877.029.390 1
1 W 20 Leonie 43 V 2 left 3.019.711.804.252.690 1

#6

Does this look like what you're getting? (n.b. I'm using the reprex package here, which ensures that you can run the code and get the same thing. I copied and pasted the data from the table you have above using the datapasta package. For on using reprex in the community site, check out the reprex FAQ.).

dat <- tibble::tribble(
  ~participant, ~gender, ~age,    ~name, ~stimulusNr, ~stimulusFinalSound, ~stimulusSyllableCount, ~response,           ~reactionTime, ~accuracy,
             1,     "W",   20, "Leonie",          27,                 "V",                      2,   "right", "4.658.930.309.931.340",         1,
             1,     "W",   20, "Leonie",          48,                 "K",                      2,    "left", "8.419.140.877.029.390",         1,
             1,     "W",   20, "Leonie",          43,                 "V",                      2,    "left", "3.019.711.804.252.690",         1
  )

tibble::glimpse(dat)
#> Observations: 3
#> Variables: 10
#> $ participant           <dbl> 1, 1, 1
#> $ gender                <chr> "W", "W", "W"
#> $ age                   <dbl> 20, 20, 20
#> $ name                  <chr> "Leonie", "Leonie", "Leonie"
#> $ stimulusNr            <dbl> 27, 48, 43
#> $ stimulusFinalSound    <chr> "V", "K", "V"
#> $ stimulusSyllableCount <dbl> 2, 2, 2
#> $ response              <chr> "right", "left", "left"
#> $ reactionTime          <chr> "4.658.930.309.931.340", "8.419.140.877.02…
#> $ accuracy              <dbl> 1, 1, 1

Created on 2018-10-08 by the reprex package (v0.2.1.9000)


#7

Thank your the quick answer!

Although we thought there would be a ridiculously simple solution to our problem (no offense) this might be a way to solve it.
I tried your code out and it works so far. We managed to get the columns out of dat using rt <- dat$reactionTime and ac <- dat.accuracy but when we combine the variables to get a visible output with barplot(rt$ac) we struggle with $ operator is invalid for atomic vectors again.

Do you have additional advice?


#8

Could you please turn this into a self-contained reprex (short for reproducible example)? It will help us help you if we can be sure we're all working with/looking at the same stuff.

install.packages("reprex")

If you've never heard of a reprex before, you might want to start by reading the tidyverse.org help page. The reprex dos and don'ts are also useful.

What to do if you run into clipboard problems

If you run into problems with access to your clipboard, you can specify an outfile for the reprex, and then copy and paste the contents into the forum.

reprex::reprex(input = "fruits_stringdist.R", outfile = "fruits_stringdist.md")

#9

Hi Malte and Vanessa!

There may well be a simple answer to your problem! I think right now people here aren’t sure exactly what your problem is yet. :confused: It’s quite difficult to understand code problems based on a verbal description. And since there wasn’t initially any sample data available, helpers had to guess what might work — which isn’t very efficient for you or for them! :upside_down_face:

@mara wasn’t trying to solve your problem with the code she posted, she was trying to show you how to get your sample data into a format everybody could work with (again, making some guesses!). It sounds like her guesses might not match up with your actual problem. For instance, you said:

But @mara’s example winds up with reactionTime as a character vector.

I think the best thing you can do now is to take @mara’s advice and make your own reproducible example of your problem. There seem to be a lot of moving parts (importing from a CSV, plotting with barplot()) and without being able to both see and run all of the code, it’s almost impossible for helpers to, well, help :sweat_smile:.

If you get stuck on that, I think at least providing these key elements will be a better starting point:

  • All the code you are trying to run, from the data import step through to the plotting steps (don’t forget your library() calls, if any!)
  • The output of calling str() on the data frame you wind up with just after importing (before you’ve done anything else)

#10

Hi and thank you for your kind approach to our problem!
I totally agree with you and although Vanessa and I have started using MS Excel for visualizing our data I hope we can fix the issues with RStudio to have it working and be the best solution there.

I have used reprex() now and this is what it rendered:

library(reprex)

Perzeptionsdaten <- read.table("C:\\Users\\Public\\Gesamtergebnisse Perzeption.txt", header=TRUE)
# Zunächst verschaffen wir uns einen Überblick über die Daten, gucken uns die ersten 6 Zeilen an. Das macht man mit dem Befehl head()
head(Perzeptionsdaten, n=1)
#>   participant gender age   name subject
#> 1           1      W  20 Leonie    test
#>                                                                         stimulus
#> 1 27-traintest-2-V-lastcorrect|\\FIBilder/m28double.jpg|\\FIBilder/m28double.jpg
#>   stimulusNr stimulusFinalSound stimulusSyllableCount response
#> 1         27                  V                     2    right
#>            reactionTime accuracy
#> 1 4.658.930.309.931.340        1

# Eine andere Art der Ăśbersicht bietet dieser Befehl: str()
str(Perzeptionsdaten)
#> 'data.frame':    320 obs. of  12 variables:
#>  $ participant          : int  1 1 1 1 1 1 1 1 1 1 ...
#>  $ gender               : Factor w/ 2 levels "M","W": 2 2 2 2 2 2 2 2 2 2 ...
#>  $ age                  : int  20 20 20 20 20 20 20 20 20 20 ...
#>  $ name                 : Factor w/ 10 levels "Gerhard","Hanna",..: 5 5 5 5 5 5 5 5 5 5 ...
#>  $ subject              : Factor w/ 1 level "test": 1 1 1 1 1 1 1 1 1 1 ...
#>  $ stimulus             : Factor w/ 48 levels "17-traintest-1-V-firstcorrect|\\FIBilder/m18double.jpg|\\FIBilder/m18double.jpg",..: 17 47 39 36 1 18 29 30 26 34 ...
#>  $ stimulusNr           : int  27 48 43 40 17 28 35 36 33 38 ...
#>  $ stimulusFinalSound   : Factor w/ 2 levels "K","V": 2 1 2 1 2 2 2 2 2 1 ...
#>  $ stimulusSyllableCount: int  2 2 2 1 1 2 1 1 1 1 ...
#>  $ response             : Factor w/ 2 levels "left","right": 2 1 1 2 1 2 2 2 2 2 ...
#>  $ reactionTime         : Factor w/ 320 levels "1.020.389.836.443.520",..: 246 315 135 145 103 217 1 22 73 268 ...
#>  $ accuracy             : int  1 1 1 1 1 1 1 1 1 1 ...

# Hier werden alle f?r die Auswertung m?glicherweise wichtigen Spalten der Tabelle als Vektoren in Variablen geschrieben

stimulusRaw <- Perzeptionsdaten$stimulus
stimulusNr <- Perzeptionsdaten$stimulusNr
stimulusSyllableCount <- Perzeptionsdaten$stimulusSyllableCount
stimulusFinalSound <- Perzeptionsdaten$stimulusFinalSound
accuracy <- Perzeptionsdaten$accuracy
reactionTime <- Perzeptionsdaten$reactionTime
participantNr <- Perzeptionsdaten$participant
age <- Perzeptionsdaten$age
gender <- Perzeptionsdaten$gender
correctOnes  <- subset(Perzeptionsdaten,accuracy==1)
incorrectOnes  <- subset(Perzeptionsdaten,accuracy==0)
correctOnes$reactionTime
#>   [1] 4.658.930.309.931.340  8.419.140.877.029.390  3.019.711.804.252.690 
#>   [4] 3.219.001.448.158.170  26.360.457.614.048.000 38.431.681.778.884.800
#>   [7] 1.020.389.836.443.520  2.317.648.975.318.010  22.947.483.931.031.600
#>  [10] 5.262.961.304.965.300  3.260.026.500.467.850  27.254.559.657.415.300
#>  [13] 3.889.606.200.035.450  2.982.285.350.740.610  5.104.286.892.535.180 
#>  [16] 28.120.692.552.262.500 30.839.754.287.032.300 32.120.851.606.399.200
#>  [19] 4.179.230.229.510.700  4.275.104.571.643.790  29.550.575.993.936.100
#>  [22] 30.279.999.333.031.200 4.890.985.096.368.730  3.124.391.842.584.500 
#>  [25] 34.675.799.022.345.600 22.734.761.912.681.700 26.260.298.431.642.700
#>  [28] 26.909.098.022.278.000 24.216.889.491.977.100 36.920.898.809.522.500
#>  [31] 3.378.930.976.932.930  25.152.845.849.679.700 2.908.127.022.644.370 
#>  [34] 3.842.754.508.995.320  28.519.424.480.744.100 26.032.812.602.267.100
#>  [37] 24.532.464.350.712.700 27.123.481.129.599.500 3.532.098.141.510.270 
#>  [40] 3.492.549.470.315.210  2.675.858.667.612.970  3.169.885.160.378.110 
#>  [43] 27.555.312.983.881.100 2.923.123.001.408.860  2.916.124.621.445.180 
#>  [46] 25.518.432.958.026.600 27.188.077.613.230.600 20.655.931.370.769.700
#>  [49] 26.547.852.833.870.400 2.642.596.481.051.660  2.637.851.794.939.610 
#>  [52] 24.647.105.151.423.500 26.811.889.036.736.700 22.716.079.470.615.800
#>  [55] 6.399.124.432.887.930  3.572.576.124.698.780  4.175.007.599.963.000 
#>  [58] 4.540.413.848.480.020  3.167.546.167.373.100  3.141.236.825.125.610 
#>  [61] 3.196.867.275.774.810  3.825.252.786.973.590  29.980.188.786.937.500
#>  [64] 3.757.362.344.456.770  26.110.133.413.030.600 4.986.958.847.319.790 
#>  [67] 547.593.409.003.655    1.804.713.003.569.630  32.519.089.697.525.100
#>  [70] 32.319.502.468.545.200 35.727.101.662.496.200 38.650.740.306.995.100
#>  [73] 2.608.237.590.894.430  3.091.445.840.157.610  27.797.607.518.644.700
#>  [76] 2.940.035.325.399.920  3.271.185.299.297.390  31.824.048.978.406.900
#>  [79] 32.163.180.517.410.200 2.676.624.436.095.680  2.682.138.738.777.740 
#>  [82] 3.097.216.040.348.980  2.513.234.198.168.680  2.604.194.538.544.880 
#>  [85] 30.240.447.455.061.900 23.514.017.924.364.900 6.276.742.572.237.690 
#>  [88] 4.484.093.949.660.680  26.641.951.285.853.500 3.109.964.097.387.900 
#>  [91] 2.500.861.329.205.190  2.718.074.060.203.680  31.563.783.896.785.500
#>  [94] 229.196.430.518.515    57.315.622.318.384.000 29.006.713.621.820.900
#>  [97] 27.507.917.430.387.000 2.522.332.348.650.340  2.496.069.824.745.710 
#> [100] 33.306.317.654.102.300 24.629.301.354.907.400 2.321.921.629.944.650 
#> [103] 2.876.188.576.581.310  2.350.196.701.166.150  2.500.413.668.903.050 
#> [106] 36.184.638.709.904.300 22.898.279.778.709.700 298.078.523.977.982   
#> [109] 52.511.682.138.105.400 68.222.807.843.403.000 813.970.913.272.351   
#> [112] 6.284.071.922.302.240  3.572.070.034.686.470  5.835.378.632.880.740 
#> [115] 21.560.372.533.276.600 27.965.032.930.951.500 24.838.574.796.449.300
#> [118] 2.763.275.744.859.120  2.604.123.395.634.810  44.835.878.261.365    
#> [121] 33.320.655.634.161.000 3.262.018.655.589.780  237.974.645.732.902   
#> [124] 1.396.997.471.570.500  5.636.070.942.739.020  2.467.755.065.765.230 
#> [127] 34.040.391.712.915.100 23.958.885.511.383.400 40.912.312.243.599.400
#> [130] 30.520.363.110.117.600 4.219.922.534.190.110  4.467.923.172.749.570 
#> [133] 30.913.754.377.979.700 2.835.762.206.465.000  21.643.640.869.297.000
#> [136] 3.323.639.316.018.670  25.238.169.801.887.100 25.797.107.880.935.000
#> [139] 39.396.564.818.453.000 25.161.177.737.172.600 196.400.124.928.914   
#> [142] 2.011.481.183.115.390  7.659.187.563.458.160  4.611.501.578.561.530 
#> [145] 5.171.540.549.808.630  7.459.544.858.870.470  41.076.017.996.413.500
#> [148] 3.291.618.138.403.970  4.267.544.227.517.350  25.556.348.876.893.600
#> [151] 40.356.044.383.059.100 249.963.159.219.004    27.685.884.035.422.800
#> [154] 65.795.251.255.186.000 21.065.301.965.232.600 2.306.074.517.468.730 
#> [157] 20.407.763.035.569.100 2.314.813.674.477.230  26.596.083.481.697.700
#> [160] 28.674.632.556.566.600 2.051.735.108.768.100  9.404.789.605.675.310 
#> [163] 26.713.880.514.884.800 6.559.917.122.075.710  6.869.077.837.437.650 
#> [166] 7.212.777.642.812.050  4.296.121.640.971.810  4.015.923.719.400.800 
#> [169] 4.235.919.134.111.890  3.830.391.428.984.280  3.399.760.810.589.210 
#> [172] 4.272.990.415.298.860  311.665.155.338.801    34.497.462.675.967.600
#> [175] 40.759.701.886.747.700 39.056.536.304.672.100 5.799.734.804.175.040 
#> [178] 3.917.731.498.702.890  47.532.054.461.897.800 24.626.700.246.296.800
#> [181] 24.675.379.915.283.800 4.373.675.133.466.460  4.668.003.367.193.810 
#> [184] 51.605.576.980.299.400 4.511.299.732.744.870  22.029.712.186.645.100
#> [187] 41.465.280.234.865.300 50.395.257.958.030.000 15.378.173.537.247.100
#> [190] 35.262.506.116.514.400 2.491.539.743.493.140  366.078.388.437.154   
#> [193] 32.237.536.656.506.300 4.394.129.764.596.860  3.292.062.606.802.570 
#> [196] 3.624.186.191.457.850  4.253.405.626.320.720  3.919.237.125.925.740 
#> [199] 29.843.876.716.169.000 1.137.978.661.156.060  10.259.742.948.247.500
#> [202] 7.021.441.159.006.820  10.087.444.437.129.500 6.282.843.377.104.080 
#> [205] 3.623.517.555.990.700  1.670.033.055.355.040  3.598.702.765.220.530 
#> [208] 5.385.147.329.010.690  39.066.849.852.309.800 7.662.778.840.483.950 
#> [211] 3.845.336.148.488.740  61.973.333.288.595.900 5.162.962.595.390.120 
#> [214] 6.940.300.167.223.650  4.498.442.360.224.650  7.636.489.903.762.480 
#> [217] 6.222.683.301.483.920  574.178.457.890.855    6.529.712.869.931.920 
#> [220] 616.055.359.169.809    3.377.037.472.966.540  6.684.512.364.531.410 
#> [223] 3.306.969.904.144.200  35.629.597.657.287.100 3.292.502.395.431.540 
#> [226] 2.912.762.038.406.410  3.197.960.131.296.570  433.843.567.737.159   
#> [229] 27.107.736.057.433.500 3.576.425.490.213.670  244.356.016.926.436   
#> [232] 2.987.771.434.142.030  26.037.366.166.886.700 3.299.772.065.227.580 
#> [235] 2.594.620.508.485.610  5.483.966.962.926.260  27.551.913.843.990.800
#> [238] 4.052.131.902.017.600  284.947.839.237.359    28.260.012.390.826.800
#> [241] 3.430.596.619.587.470  5.219.400.366.080.980  6.418.996.573.923.580 
#> [244] 28.282.882.829.725.600 2.995.327.786.108.170  6.340.116.648.217.300 
#> [247] 33.285.711.170.599.800 32.299.787.457.559.000 36.998.540.293.916.400
#> [250] 54.842.376.114.179.800 2.979.200.471.005.380  5.532.223.204.195.920 
#> 320 Levels: 1.020.389.836.443.520 ... 9.404.789.605.675.310

mean(correctOnes$reactionTime)
#> Warning in mean.default(correctOnes$reactionTime): Argument ist weder
#> numerisch noch boolesch: gebe NA zurĂĽck
#> [1] NA
barplot(correctOnes$correctOnes$reactionTime)
#> Error in barplot.default(correctOnes$correctOnes$reactionTime): 'height' must be a vector or a matrix

Created on 2018-10-09 by the reprex package (v0.2.1)

Here I have uploaded the txt data file https://drive.google.com/open?id=1pwBK2Zdcn_WRKWZ3J-Votm338ePegMms

Please don't get confused by the parts which feature German language :sweat_smile:
If you need anything more, don't hesitate to tell me.
Thanks again!