Automatically print message when data.frame updates - r

I'd like for R to print the dimensions of my updated dataframes to show how each of my steps influences the number of observations remaining. Typically my dataset size changes because there are NAs. I can always quickly check dimensions:
a = data.frame(a = c(1,2), b = c(3, NA))
na.omit(a) %>% dim
but I would want to do two things in that one line:
na.omit(a) %>% size
a <- na.omit(a)
since code gets messy with many dim() calls.
Is there a way to toggle R's output? Something like
a <- na.omit(a)
message: dimensions are now m x n

Edit: Added code to only display notification when the object changes under the original code.
If you just want the dimensions to print after every line that you execute you could write a taskCallBack
tc <- addTaskCallback(function(...){message("Dimensions are: ", paste(dim(a), collapse = " "));return(TRUE)})
# When you want to remove the callback
removeTaskCallback(tc)
Here is an example session of it in use. You could probably modify the taskCallBack to only print if things change.
> a <- mtcars
>
> tc <- addTaskCallback(function(...){message("Dimensions are: ", paste(dim(a), collapse = " "));return(TRUE)})
Dimensions are: 32 11
>
> a
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4
Merc 450SE 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3
Merc 450SL 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3
Merc 450SLC 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3
Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4
Lincoln Continental 10.4 8 460.0 215 3.00 5.424 17.82 0 0 3 4
Chrysler Imperial 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4
Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1
Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2
Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1
Toyota Corona 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1
Dodge Challenger 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2
AMC Javelin 15.2 8 304.0 150 3.15 3.435 17.30 0 0 3 2
Camaro Z28 13.3 8 350.0 245 3.73 3.840 15.41 0 0 3 4
Pontiac Firebird 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3 2
Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1
Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2
Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2
Ford Pantera L 15.8 8 351.0 264 4.22 3.170 14.50 0 1 5 4
Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6
Maserati Bora 15.0 8 301.0 335 3.54 3.570 14.60 0 1 5 8
Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2
Dimensions are: 32 11
> a <- a[1:5,]
Dimensions are: 5 11
>
> # When you want to remove the callback
> removeTaskCallback(tc)
[1] TRUE
If you want to only receive notifications when the object changes we can write a little bit more to actually monitor the object and only display the message when appropriate. Here is the code I'll use:
# #object_name - Character string. The object name you want to montior
# #example tc <- addTaskCallback(monitor_object("mtcars"))
monitor_object <- function(object_name){
object_dim <- NULL
if(exists(object_name)){
object_dim <- dim(get(object_name))
}
f <- function(...){
new_dim <- NULL
if(exists(object_name)){
new_dim <- dim(get(object_name))
}
if(!identical(new_dim, object_dim)){
msg <- paste0(object_name,
" changed.\nOld dimensions: ",
paste(object_dim, collapse = " "),
"\nNew dimensions: ",
paste(new_dim, collapse = " "))
object_dim <<- new_dim
message(msg)
}
return(TRUE)
}
return(f)
}
tc <- addTaskCallback(monitor_object("mtcars"))
# When you want to remove the callback
removeTaskCallback(tc)
And an example session...
> tc <- addTaskCallback(monitor_object("mtcars"))
> mtcars <- mtcars[,-1]
mtcars changed.
Old dimensions: 32 11
New dimensions: 32 10
> head(mtcars)
cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 6 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 6 160 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 4 108 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 6 258 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 8 360 175 3.15 3.440 17.02 0 0 3 2
Valiant 6 225 105 2.76 3.460 20.22 1 0 3 1
> mtcars <- mtcars[1:5,]
mtcars changed.
Old dimensions: 32 10
New dimensions: 5 10
I've actually expanded upon this code because it was a fun little exercise and made it so you can specify the function you want to use to monitor the object. I made a gist for the function that can be found here.

One option would be to write a new function like so:
omitter<-function(x){
x<-na.omit(x)
print(paste0("The dimensions are now ",dim(x)[1]," x ", dim(x)[2]))
return(x)
}
This would print the message and store the new object:
y<-omitter(data.frame(a=c(1:5,NA)))
> y
a
1 1
2 2
3 3
4 4
5 5
Edit: based on #DavidKlotz comment above, you could use message in place of print

Related

Sort columns in order of decreasing variance in R

I wish to order the columns of a dataset in order of decreasing column variance but I have had no luck in doing so. This is what I have so far:
og_data <- og_data[, sort(apply(og_data, 2, var), decreasing=TRUE)]
Now, I know this doesn't work since sort(apply(og_data, 2, var), decreasing=TRUE) returns the variance values of the columns in order of decreasing variance. I have no idea how to extract the column indexes from this which is what I would need to use. Any help would be much appreciated.
Since you did not give reproducible data for me to work with
you can try this method below
# sorting examples using the mtcars dataset
attach(mtcars)
# sort by mpg
new_data <- mtcars[order(mpg),]
# sort by mpg and cyl
new_data <- mtcars[order(mpg, cyl),]
#sort by mpg (ascending) and cyl (descending)
new_data <- mtcars[order(mpg, -cyl),]
hope this solved your question
Since the goal is to order the columns of the data frame by descending variance, we calculate the variances and use order() to sort by descending variance.
We'll use mtcars to illustrate, given the absence of a minimal reproducible example:
mtcars[,order(apply(mtcars,2,var),decreasing=TRUE)]
...and the output:
disp hp mpg qsec cyl carb wt gear drat vs am
Mazda RX4 160.0 110 21.0 16.46 6 4 2.620 4 3.90 0 1
Mazda RX4 Wag 160.0 110 21.0 17.02 6 4 2.875 4 3.90 0 1
Datsun 710 108.0 93 22.8 18.61 4 1 2.320 4 3.85 1 1
Hornet 4 Drive 258.0 110 21.4 19.44 6 1 3.215 3 3.08 1 0
Hornet Sportabout 360.0 175 18.7 17.02 8 2 3.440 3 3.15 0 0
Valiant 225.0 105 18.1 20.22 6 1 3.460 3 2.76 1 0
Duster 360 360.0 245 14.3 15.84 8 4 3.570 3 3.21 0 0
Merc 240D 146.7 62 24.4 20.00 4 2 3.190 4 3.69 1 0
Merc 230 140.8 95 22.8 22.90 4 2 3.150 4 3.92 1 0
Merc 280 167.6 123 19.2 18.30 6 4 3.440 4 3.92 1 0
Merc 280C 167.6 123 17.8 18.90 6 4 3.440 4 3.92 1 0
Merc 450SE 275.8 180 16.4 17.40 8 3 4.070 3 3.07 0 0
Merc 450SL 275.8 180 17.3 17.60 8 3 3.730 3 3.07 0 0
Merc 450SLC 275.8 180 15.2 18.00 8 3 3.780 3 3.07 0 0
Cadillac Fleetwood 472.0 205 10.4 17.98 8 4 5.250 3 2.93 0 0
Lincoln Continental 460.0 215 10.4 17.82 8 4 5.424 3 3.00 0 0
Chrysler Imperial 440.0 230 14.7 17.42 8 4 5.345 3 3.23 0 0
Fiat 128 78.7 66 32.4 19.47 4 1 2.200 4 4.08 1 1
Honda Civic 75.7 52 30.4 18.52 4 2 1.615 4 4.93 1 1
Toyota Corolla 71.1 65 33.9 19.90 4 1 1.835 4 4.22 1 1
Toyota Corona 120.1 97 21.5 20.01 4 1 2.465 3 3.70 1 0
Dodge Challenger 318.0 150 15.5 16.87 8 2 3.520 3 2.76 0 0
AMC Javelin 304.0 150 15.2 17.30 8 2 3.435 3 3.15 0 0
Camaro Z28 350.0 245 13.3 15.41 8 4 3.840 3 3.73 0 0
Pontiac Firebird 400.0 175 19.2 17.05 8 2 3.845 3 3.08 0 0
Fiat X1-9 79.0 66 27.3 18.90 4 1 1.935 4 4.08 1 1
Porsche 914-2 120.3 91 26.0 16.70 4 2 2.140 5 4.43 0 1
Lotus Europa 95.1 113 30.4 16.90 4 2 1.513 5 3.77 1 1
Ford Pantera L 351.0 264 15.8 14.50 8 4 3.170 5 4.22 0 1
Ferrari Dino 145.0 175 19.7 15.50 6 6 2.770 5 3.62 0 1
Maserati Bora 301.0 335 15.0 14.60 8 8 3.570 5 3.54 0 1
Volvo 142E 121.0 109 21.4 18.60 4 2 2.780 4 4.11 1 1
>
To cross-check the results, we'll sort and print the vector of variances:
#cross-check variances
variances <- apply(mtcars,2,var)
variances[order(variances,decreasing = TRUE)]
Notice that the ordering of the vector matches the ordering of the columns from the prior operation.
disp hp mpg qsec cyl carb
1.536080e+04 4.700867e+03 3.632410e+01 3.193166e+00 3.189516e+00 2.608871e+00
wt gear drat vs am
9.573790e-01 5.443548e-01 2.858814e-01 2.540323e-01 2.489919e-01
>

Using ifelse() statements to get objects of different classes in R

I want to know how to use ifelse() statements to get different kinds of objects in R.
Specifically, I want to do this with the mtcars data, which is a list type object. I used the code below to create a character object version of the name of the data, called mtcars__string_object.
mtcars__string_object <- c("mtcars")
For the object itself, here is the code I want to use to get this data via an ifelse() statement, which does not work:
test_1 <-
ifelse(
((typeof(mtcars) == "list") == TRUE),
(mtcars),
NA
)
It does not work because it doesn't give me the dataset in its original form. Here are the results:
> test_1
[[1]]
[1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3 15.2 10.4
[16] 10.4 14.7 32.4 30.4 33.9 21.5 15.5 15.2 13.3 19.2 27.3 26.0 30.4 15.8 19.7
[31] 15.0 21.4
When I just use the name of the object, here is the code I want to use to get this data via an ifelse() statement, which does not work:
test_2 <-
ifelse(
((typeof(mtcars) == "character") == TRUE),
(get(mtcars__string_object[1])),
NA
)
It does not work because it doesn't give me the dataset in its original form. Here are the results:
> test_2
[1] NA
I'm not sure how to fix the code to get the desired result. Please advise. Thanks.
Here is the code that I used for the example:
# StackOverflow materials
## sets up data
### name of object
mtcars
### text object with name of dataset
mtcars__string_object <- c("mtcars")
## checks typeof() of objects
typeof(mtcars)
typeof(mtcars__string_object)
## ifelse() statements to call data
### for object itself
# ---- NOTE: creates object
test_1 <-
ifelse(
((typeof(mtcars) == "list") == TRUE),
(mtcars),
NA
)
# ---- NOTE: displays object
test_1
# ---- NOTE: does not work
### for text object with name of dataset
# ---- NOTE: creates object
test_2 <-
ifelse(
((typeof(mtcars) == "character") == TRUE),
(get(mtcars__string_object[1])),
NA
)
# ---- NOTE: displays object
test_2
# ---- NOTE: does not work
ifelse requires all the arguments to be same length. Here, it is not the case. Therefore, we need if/else
if(is.data.frame(mtcars) ) get(mtcars__string_object) else NA
-ouptut
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4
Merc 450SE 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3
Merc 450SL 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3
Merc 450SLC 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3
Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4
Lincoln Continental 10.4 8 460.0 215 3.00 5.424 17.82 0 0 3 4
Chrysler Imperial 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4
Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1
Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2
Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1
Toyota Corona 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1
Dodge Challenger 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2
AMC Javelin 15.2 8 304.0 150 3.15 3.435 17.30 0 0 3 2
Camaro Z28 13.3 8 350.0 245 3.73 3.840 15.41 0 0 3 4
Pontiac Firebird 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3 2
Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1
Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2
Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2
Ford Pantera L 15.8 8 351.0 264 4.22 3.170 14.50 0 1 5 4
Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6
Maserati Bora 15.0 8 301.0 335 3.54 3.570 14.60 0 1 5 8
Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2
If you really want to use ifelse, below is a workaround using list
> ifelse(is.data.frame(mtcars), list(get(mtcars__string_object)), NA)[[1]]
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4
Merc 450SE 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3
Merc 450SL 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3
Merc 450SLC 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3
Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4
Lincoln Continental 10.4 8 460.0 215 3.00 5.424 17.82 0 0 3 4
Chrysler Imperial 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4
Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1
Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2
Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1
Toyota Corona 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1
Dodge Challenger 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2
AMC Javelin 15.2 8 304.0 150 3.15 3.435 17.30 0 0 3 2
Camaro Z28 13.3 8 350.0 245 3.73 3.840 15.41 0 0 3 4
Pontiac Firebird 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3 2
Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1
Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2
Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2
Ford Pantera L 15.8 8 351.0 264 4.22 3.170 14.50 0 1 5 4
Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6
Maserati Bora 15.0 8 301.0 335 3.54 3.570 14.60 0 1 5 8
Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2
Anyway, if ... else ... is a more proper way for your case, as #akrun's answer gives.
use `if`
test_3 <-
`if`(
((typeof(mtcars) == "list") == TRUE),
(mtcars),
NA
)
test_3
you will get answer.
> test_3
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4

PCA analysis: getting error in dim desc(): not convenient data

I have conducted PCA on a set of data using prcomp. As a final step I am trying to use the dimdesc() function from FactoMineR to obtain p-values that identify the most significantly associated variables with my principal components.
The data frame has seven variables all of which are numerical and there are no missing values. The names are standard names such as "RCH_Home" (just in case the names could be problematic).
I write the following function:
res.desc <- dimdesc(df_PCA, axes = c(1:2), proba = 0.05)
And get the following error message:
Error in dimdesc(df_PCA, axes = c(1:2), proba = 0.05) : non convenient data
Any idea what might be going on?
Thanks!!!!
You should use the PCA function in sostitution of the prcomp
Below an example of PCA with FactoMineR.
library(FactoMineR)
library(factoextra)
library(paran)
data(cars)
mtcars_pca<-cars_pca<-PCA(mtcars)
If you want to check the percentage of variance, you can do this:
mtcars_pca$eig
> mtcars_pca$eig
eigenvalue percentage of variance cumulative percentage of variance
comp 1 6.60840025 60.0763659 60.07637
comp 2 2.65046789 24.0951627 84.17153
comp 3 0.62719727 5.7017934 89.87332
comp 4 0.26959744 2.4508858 92.32421
comp 5 0.22345110 2.0313737 94.35558
comp 6 0.21159612 1.9236011 96.27918
comp 7 0.13526199 1.2296544 97.50884
comp 8 0.12290143 1.1172858 98.62612
comp 9 0.07704665 0.7004241 99.32655
comp 10 0.05203544 0.4730495 99.79960
comp 11 0.02204441 0.2004037 100.00000
Cos2 stands for squared cosine and is an index for the quality representation of both variables and individuals. The closer this value is to one, the better the quality.
mtcars_pca$var$cos2
Dim.1 Dim.2 Dim.3 Dim.4 Dim.5
mpg 0.8685312 0.0006891117 0.031962249 1.369725e-04 0.0023634487
cyl 0.9239416 0.0050717032 0.019276287 1.811054e-06 0.0007642822
disp 0.8958370 0.0064482423 0.002370993 1.775235e-02 0.0346868281
hp 0.7199031 0.1640467049 0.012295659 1.234773e-03 0.0651697911
drat 0.5717921 0.1999959326 0.016295731 1.970035e-01 0.0013361275
wt 0.7916038 0.0542284172 0.073281663 1.630161e-02 0.0012578888
qsec 0.2655437 0.5690984542 0.101947952 1.249426e-03 0.0060588455
vs 0.6208539 0.1422249798 0.115330572 1.244460e-02 0.0803189801
am 0.3647715 0.4887450097 0.026555457 2.501834e-04 0.0018011675
gear 0.2829342 0.5665806069 0.052667265 1.888829e-02 0.0005219259
carb 0.3026882 0.4533387304 0.175213444 4.333912e-03 0.0291718181
res.desc <- dimdesc(mtcars_pca, axes = c(1:2), proba = 0.05)
> head(res.desc)
$Dim.1
$quanti
correlation p.value
cyl 0.9612188 2.471950e-18
disp 0.9464866 2.804047e-16
wt 0.8897212 9.780198e-12
hp 0.8484710 8.622043e-10
carb 0.5501711 1.105272e-03
qsec -0.5153093 2.542578e-03
gear -0.5319156 1.728737e-03
am -0.6039632 2.520665e-04
drat -0.7561693 5.575736e-07
vs -0.7879428 8.658012e-08
mpg -0.9319502 9.347042e-15
attr(,"class")
[1] "condes" "list "
$Dim.2
$quanti
correlation p.value
gear 0.7527155 6.712704e-07
am 0.6991030 8.541542e-06
carb 0.6733043 2.411011e-05
drat 0.4472090 1.028069e-02
hp 0.4050268 2.147312e-02
vs -0.3771273 3.335771e-02
qsec -0.7543861 6.138696e-07
attr(,"class")
[1] "condes" "list "
$call
$call$num.var
[1] 1
$call$proba
[1] 0.05
$call$weights
[1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
$call$X
Dim.1 mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 -0.6572132031 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag -0.6293955058 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 -2.7793970426 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive -0.3117707086 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 1.9744889419 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
Valiant -0.0561375337 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
Duster 360 3.0026742880 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
Merc 240D -2.0553287289 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
Merc 230 -2.2874083842 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
Merc 280 -0.5263812077 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
Merc 280C -0.5092054932 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4
Merc 450SE 2.2478104359 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3
Merc 450SL 2.0478227622 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3
Merc 450SLC 2.1485421615 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3
Cadillac Fleetwood 3.8997903717 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4
Lincoln Continental 3.9541231097 10.4 8 460.0 215 3.00 5.424 17.82 0 0 3 4
Chrysler Imperial 3.5929719882 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4
Fiat 128 -3.8562837567 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1
Honda Civic -4.2540325032 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2
Toyota Corolla -4.2342207436 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1
Toyota Corona -1.9041678566 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1
Dodge Challenger 2.1848507430 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2
AMC Javelin 1.8633834347 15.2 8 304.0 150 3.15 3.435 17.30 0 0 3 2
Camaro Z28 2.8889945733 13.3 8 350.0 245 3.73 3.840 15.41 0 0 3 4
Pontiac Firebird 2.2459189274 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3 2
Fiat X1-9 -3.5739682964 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1
Porsche 914-2 -2.6512550541 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2
Lotus Europa -3.3857059882 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2
Ford Pantera L 1.3729574238 15.8 8 351.0 264 4.22 3.170 14.50 0 1 5 4
Ferrari Dino -0.0009899207 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6
Maserati Bora 2.6691258658 15.0 8 301.0 335 3.54 3.570 14.60 0 1 5 8
Volvo 142E -2.4205931001 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2
it should be use the function from the same package.
If you want to choose how many dimensions you need, you can do this by the param package
library(paran)
cars_paran<-paran(mtcars, graph = TRUE)

subsetting data in a-z orderly fashion

I would like to subset a data into few pieces given a cutoff keyword.
Would prefer flexible sub-setting (ie cutoff varies depending on user input). In this case: two cutoff keywords resulting in three output tables. Thanks!
keyword_cutoff <- c("Merc 240D", "Fiat 128")
data input:
library(data.table)
tmp_mtcars <- setDT(mtcars, keep.rownames =TRUE)[]
colnames(tmp_mtcars)[1] <- "cartype"
desired output table #1:
cartype mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
desired output table #2:
Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4
Merc 450SE 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3
Merc 450SL 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3
Merc 450SLC 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3
Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4
Lincoln Continental 10.4 8 460.0 215 3.00 5.424 17.82 0 0 3 4
Chrysler Imperial 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4
desired output table #3:
Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1
Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2
Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1
Toyota Corona 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1
Dodge Challenger 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2
AMC Javelin 15.2 8 304.0 150 3.15 3.435 17.30 0 0 3 2
Camaro Z28 13.3 8 350.0 245 3.73 3.840 15.41 0 0 3 4
Pontiac Firebird 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3 2
Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1
Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2
Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2
Ford Pantera L 15.8 8 351.0 264 4.22 3.170 14.50 0 1 5 4
Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6
Maserati Bora 15.0 8 301.0 335 3.54 3.570 14.60 0 1 5 8
Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2
We need a grouping variable to split. This can be done by converting a logical vector (cartype %in% keyword_cutoff) into numeric index by taking the cumulative sum of the logical index and split it to a list of data.tables
lst <- split(tmp_mtcars, tmp_mtcars[, cumsum(cartype %in% keyword_cutoff)])
lst[[1]]
# cartype mpg cyl disp hp drat wt qsec vs am gear carb
#1: Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
#2: Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
#3: Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
#4: Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
#5: Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
#6: Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
#7: Duster 360 14.3 8 360 245 3.21 3.570 15.84 0 0 3 4

R: substituting one value with another in a data frame

Simple question here: I'm just trying to replace a certain value within a specific column of a data frame with another value. I tried the advice given in: Replacing values from a column using a condition in R and Replace a single column values but it didn't work for me.
> x <- mtcars
> x
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4
Merc 450SE 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3
Merc 450SL 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3
Merc 450SLC 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3
Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4
Lincoln Continental 10.4 8 460.0 215 3.00 5.424 17.82 0 0 3 4
Chrysler Imperial 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4
Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1
Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2
Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1
Toyota Corona 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1
Dodge Challenger 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2
AMC Javelin 15.2 8 304.0 150 3.15 3.435 17.30 0 0 3 2
Camaro Z28 13.3 8 350.0 245 3.73 3.840 15.41 0 0 3 4
Pontiac Firebird 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3 2
Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1
Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2
Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2
Ford Pantera L 15.8 8 351.0 264 4.22 3.170 14.50 0 1 5 4
Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6
Maserati Bora 15.0 8 301.0 335 3.54 3.570 14.60 0 1 5 8
Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2
> x.new <- x[x$mpg == "21.0"] <- "9000"
> x.new
[1] "9000"
> x.new <- x$mpg[x$mpg == "21.0"] <- "9000"
> x.new
[1] "9000"
> x.new <- x[x$mpg == 21.0] <- 9000
> x.new
[1] 9000
Clearly, x.new does not return the whole data frame.
mtcars$mpg <- ifelse(mtcars$mpg==21, 9000, mtcars$mpg)
or
mtcars[mtcars$mpg==21,]$mpg <- 9000
Try this:
# load library and data
library(data.table)
data("mtcars")
# convert your dataset to data.table
setDT(mtcars)
# Solution
mtcars[mpg == 21.0, mpg := 9000]
# here is a slower solution using `ifelse`
mtcars[, mpg := ifelse(mpg==21.0 , 9000, mpg)]

Resources