jip31
March 31, 2023, 6:49am
1
Hello
I just begin with R
I try to read a txt file and to selct some colums in order to multiply one colum by another
> test <-read.table(
+ file = "SampleData2.txt",
+ header = TRUE,
+ sep = "\t",
+ quote = "\"")
>
> # Attachement du package dplyr
> library(dplyr)
>
> # Selection des colonnes
> temp <- select(
+ .test = Total)
Error in select(.data = Total) : object 'Total' not found
>
what is wrong please and how to reach my objective?
jip31:
file = "SampleData2.txt"
is missing from the reprex
. See the FAQ . So I can only offer general guidance.
Similar situation
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
mtcars %>% select(.test = MPG)
#> Error in `select()`:
#> ! Can't subset columns that don't exist.
#> ✖ Column `MPG` doesn't exist.
#> Backtrace:
#> ▆
#> 1. ├─mtcars %>% select(.test = MPG)
#> 2. ├─dplyr::select(., .test = MPG)
#> 3. ├─dplyr:::select.data.frame(., .test = MPG)
#> 4. │ └─tidyselect::eval_select(expr(c(...)), data = .data, error_call = error_call)
#> 5. │ └─tidyselect:::eval_select_impl(...)
#> 6. │ ├─tidyselect:::with_subscript_errors(...)
#> 7. │ │ └─rlang::try_fetch(...)
#> 8. │ │ └─base::withCallingHandlers(...)
#> 9. │ └─tidyselect:::vars_select_eval(...)
#> 10. │ └─tidyselect:::walk_data_tree(expr, data_mask, context_mask)
#> 11. │ └─tidyselect:::eval_c(expr, data_mask, context_mask)
#> 12. │ └─tidyselect:::reduce_sels(node, data_mask, context_mask, init = init)
#> 13. │ └─tidyselect:::walk_data_tree(new, data_mask, context_mask)
#> 14. │ └─tidyselect:::as_indices_sel_impl(...)
#> 15. │ └─tidyselect:::as_indices_impl(...)
#> 16. │ └─tidyselect:::chr_as_locations(x, vars, call = call, arg = arg)
#> 17. │ └─vctrs::vec_as_location(...)
#> 18. └─vctrs (local) `<fn>`()
#> 19. └─vctrs:::stop_subscript_oob(...)
#> 20. └─vctrs:::stop_subscript(...)
#> 21. └─rlang::abort(...)
colnames(mtcars)
#> [1] "mpg" "cyl" "disp" "hp" "drat" "wt" "qsec" "vs" "am" "gear"
#> [11] "carb"
mtcars %>% select(.test = mpg)
#> .test
#> Mazda RX4 21.0
#> Mazda RX4 Wag 21.0
#> Datsun 710 22.8
#> Hornet 4 Drive 21.4
#> Hornet Sportabout 18.7
#> Valiant 18.1
#> Duster 360 14.3
#> Merc 240D 24.4
#> Merc 230 22.8
#> Merc 280 19.2
#> Merc 280C 17.8
#> Merc 450SE 16.4
#> Merc 450SL 17.3
#> Merc 450SLC 15.2
#> Cadillac Fleetwood 10.4
#> Lincoln Continental 10.4
#> Chrysler Imperial 14.7
#> Fiat 128 32.4
#> Honda Civic 30.4
#> Toyota Corolla 33.9
#> Toyota Corona 21.5
#> Dodge Challenger 15.5
#> AMC Javelin 15.2
#> Camaro Z28 13.3
#> Pontiac Firebird 19.2
#> Fiat X1-9 27.3
#> Porsche 914-2 26.0
#> Lotus Europa 30.4
#> Ford Pantera L 15.8
#> Ferrari Dino 19.7
#> Maserati Bora 15.0
#> Volvo 142E 21.4
# alternatively to just multiply two columns
mtcars$mpg * mtcars$cyl
#> [1] 126.0 126.0 91.2 128.4 149.6 108.6 114.4 97.6 91.2 115.2 106.8 131.2
#> [13] 138.4 121.6 83.2 83.2 117.6 129.6 121.6 135.6 86.0 124.0 121.6 106.4
#> [25] 153.6 109.2 104.0 121.6 126.4 118.2 120.0 85.6
# or
mtcars[1] * mtcars[2]
#> mpg
#> Mazda RX4 126.0
#> Mazda RX4 Wag 126.0
#> Datsun 710 91.2
#> Hornet 4 Drive 128.4
#> Hornet Sportabout 149.6
#> Valiant 108.6
#> Duster 360 114.4
#> Merc 240D 97.6
#> Merc 230 91.2
#> Merc 280 115.2
#> Merc 280C 106.8
#> Merc 450SE 131.2
#> Merc 450SL 138.4
#> Merc 450SLC 121.6
#> Cadillac Fleetwood 83.2
#> Lincoln Continental 83.2
#> Chrysler Imperial 117.6
#> Fiat 128 129.6
#> Honda Civic 121.6
#> Toyota Corolla 135.6
#> Toyota Corona 86.0
#> Dodge Challenger 124.0
#> AMC Javelin 121.6
#> Camaro Z28 106.4
#> Pontiac Firebird 153.6
#> Fiat X1-9 109.2
#> Porsche 914-2 104.0
#> Lotus Europa 121.6
#> Ford Pantera L 126.4
#> Ferrari Dino 118.2
#> Maserati Bora 120.0
#> Volvo 142E 85.6
# or add to data frame
(mtcars$.test <- mtcars[1] * mtcars[2])
#> mpg
#> Mazda RX4 126.0
#> Mazda RX4 Wag 126.0
#> Datsun 710 91.2
#> Hornet 4 Drive 128.4
#> Hornet Sportabout 149.6
#> Valiant 108.6
#> Duster 360 114.4
#> Merc 240D 97.6
#> Merc 230 91.2
#> Merc 280 115.2
#> Merc 280C 106.8
#> Merc 450SE 131.2
#> Merc 450SL 138.4
#> Merc 450SLC 121.6
#> Cadillac Fleetwood 83.2
#> Lincoln Continental 83.2
#> Chrysler Imperial 117.6
#> Fiat 128 129.6
#> Honda Civic 121.6
#> Toyota Corolla 135.6
#> Toyota Corona 86.0
#> Dodge Challenger 124.0
#> AMC Javelin 121.6
#> Camaro Z28 106.4
#> Pontiac Firebird 153.6
#> Fiat X1-9 109.2
#> Porsche 914-2 104.0
#> Lotus Europa 121.6
#> Ford Pantera L 126.4
#> Ferrari Dino 118.2
#> Maserati Bora 120.0
#> Volvo 142E 85.6
# create a new data frame
(new <- cbind(mtcars[1:2],mtcars[1] * mtcars[2]))
#> mpg cyl mpg
#> Mazda RX4 21.0 6 126.0
#> Mazda RX4 Wag 21.0 6 126.0
#> Datsun 710 22.8 4 91.2
#> Hornet 4 Drive 21.4 6 128.4
#> Hornet Sportabout 18.7 8 149.6
#> Valiant 18.1 6 108.6
#> Duster 360 14.3 8 114.4
#> Merc 240D 24.4 4 97.6
#> Merc 230 22.8 4 91.2
#> Merc 280 19.2 6 115.2
#> Merc 280C 17.8 6 106.8
#> Merc 450SE 16.4 8 131.2
#> Merc 450SL 17.3 8 138.4
#> Merc 450SLC 15.2 8 121.6
#> Cadillac Fleetwood 10.4 8 83.2
#> Lincoln Continental 10.4 8 83.2
#> Chrysler Imperial 14.7 8 117.6
#> Fiat 128 32.4 4 129.6
#> Honda Civic 30.4 4 121.6
#> Toyota Corolla 33.9 4 135.6
#> Toyota Corona 21.5 4 86.0
#> Dodge Challenger 15.5 8 124.0
#> AMC Javelin 15.2 8 121.6
#> Camaro Z28 13.3 8 106.4
#> Pontiac Firebird 19.2 8 153.6
#> Fiat X1-9 27.3 4 109.2
#> Porsche 914-2 26.0 4 104.0
#> Lotus Europa 30.4 4 121.6
#> Ford Pantera L 15.8 8 126.4
#> Ferrari Dino 19.7 6 118.2
#> Maserati Bora 15.0 8 120.0
#> Volvo 142E 21.4 4 85.6
Created on 2023-03-31 with reprex v2.0.2
you read from the text file into a test
data.frame object.
You then never refer to this again; thats a mistake
to use select you must pass the object to select from as the first param either directly or indirectly with the pipe |>
(modern R4.2+) or magrittr pipe %>%
you have written the select with an equal sign , implying to do a rename at the time of selection;
the message is clear in that there is no Total column to choose and rename to be .test
you can find the names of your columns in test with the names command
names(test)
Note : R is case sensitive so Total will not match total
jip31
March 31, 2023, 11:01am
4
Thanks it works now
I have just an issue with your multipliying colums example
Do you know why I have "NA"?
> test$Units * test$Total
Error in test$Units * test$Total :
non-numeric argument to binary operator
> test$Units(as.numeric) * test$Total(as.numeric)
Error: attempt to apply non-function
> as.numeric(test$Units) * as.numeric(test$Total)
[1] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
[27] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
Warning message:
NAs introduced by coercion
either of Units or Total are not suitable to be directly interpreted as numerics; often times this is because of punctuation. if you are able to use libraries, you might find it convenient to try readr package as its parse_number()
function is somewhat more forgiving as to inputs its presented with. Aside from that ... You should explore the contents of your columns to understand what they are.
If you would like assistance consider making a reprex.
A minimal reproducible example consists of the following items:
A minimal dataset, necessary to reproduce the issue
The minimal runnable code necessary to reproduce the issue, which can be run
on the given dataset, and including the necessary information on the used packages.
Let's quickly go over each one of these with examples:
Minimal Dataset (Sample Data)
You need to provide a data frame that is small enough to be (reasonably) pasted on a post, but big enough to reproduce your issue.
Let's say, as an example, that you are working with the iris data frame
head(iris)
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1 5.1 3.5 1.4 0.…
jip31:
test$Units(as.numeric)
test$Units
is a vector
object being used as a function
object which won't work. A function
object applied to another function
object is fine so long as the return value can be used with a binary operator such as *
system
Closed
April 7, 2023, 8:21pm
7
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed. If you have a query related to it or one of the replies, start a new topic and refer back with a link.